I develope a library, when I compile my code with GCC (in Windows with CodeBlocks), The source code doesn't compile and this error appears:
error: incomplete type 'claculator' used in nested name specifier.
I write a sample code that generate this error exactly:
class claculator;
template<class T>
class my_class
{
public:
void test()
{
// GCC error: incomplete type 'claculator' used in nested name specifier
int x = claculator::add(1, 2);
}
T m_t;
};
// This class SHOULD after my_class.
// I can not move this class to top of my_class.
class claculator
{
public:
static int add(int a, int b)
{
return a+b;
}
};
int main()
{
my_class<int> c;
c.test();
return 0;
}
How can I solve this error?
Note that my source code compiled successfully in Visual Studio.
Thanks.
Its very simple. Define test()
after the definition of calculator
class as:
class calculator;
template<class T>
class my_class
{
public:
void test(); //Define it after the definition of `calculator`
T m_t;
};
// This class SHOULD after my_class.
// I can not move this class to top of my_class.
class calculator
{
public:
static int add(int a, int b)
{
return a+b;
}
};
//Define it here!
template<class T>
void my_class<T>::test()
{
int x = calculator::add(1, 2);
}
In this way, the complete definition of calculator
is known to the compiler when it parses the definition of test()
.