Function argument with nested argument

Hi,

I am trying to build a function with an array object as argument. Please see the following code:
1
2
3
// main.cpp
const int a = 10;
......


1
2
3
4
5
//function.cpp
void test_func(double x, array<double, a> &y)
{
   ......
}

This code cannot be compiled because the "a" in parameter is not identified. I cannot neither put const int a as argument to the function.

Here I need "a" to control size of the array from the main.cpp. How can I make this work, please?
constant values can be put in header files with no problems.

so

1
2
//header.h
const int a = 10;



1
2
//main.cpp
#include "header.h" 


1
2
3
4
5
6
7
//function.cpp

#include "header.h"
void test_func(double x, array<double, a> &y)
{
   ......
}
If you declare "a" in your main(), it's value and type are only defined inside the body of the function.

You have to make it a global variable, or use a macro.
Thank you for your reply. When you say "make it a global variable", you mean that I should put "a" in a header file as guestgulkan said?

However, when I put const int a = 10; in a header, and include this header in main.cpp, when I use this constant in main function like array<double, a> myArray = {}; It cannot be compiled, compiler said " 'a' is ambiguous".

Could someone tell me why and how to correct this, please?

Thanks in advance!
Last edited on
Post your actual code - you are doing something (else) wrong.
You can put the const variable in a header file.
the word ambiguous means that you have more than one instance of the same name (maybe because you have not done your header guards)

So post your code - this should a straight forward issue to resolve.
Last edited on
My code is very long, impossible to put all of them here. Anyway, I just put the part I have question with. I initialize a constant in main.cpp:
1
2
3
4
5
6
7
8
//main.cpp
const int a = 10;
......
int main()
{
   func1(m); ......
   func2(x, y); ......
}

Then I use this constant in another cpp file
1
2
3
4
5
6
7
8
9
10
//atest.cpp
extern const int a;
void func1(m)
{
   ......
}
void func2(double x, array<double, a> &y)
{
   ......
}

However, the compiler said in atest.cpp, " 'a' need a constant value". But I declared it in this cpp. Could someone please tell me why?
Last edited on
In that case refer to my earlier post about putting the const int value in a header file and including that header file (instead of putting it in a cpp file the way you are doing it now.).
Last edited on
Yes, I followed your proposition. That works.

But could you please tell me why it could not work in my way (define in a cpp and declare as "extern" in another cpp)?
To compile array<double, a>, the compiler need to have the actual value for a at the time when it is tring to compile the function
void func2(double x, array<double, a> &y)
{
......
}


extern const int a only tells the compiler that a exist somewhere - it does not give the actual value of a - and the compiler needs the actual value.
OK thanks guestgulkan, that is very clear!
Topic archived. No new replies allowed.