GCC错误:嵌套名称说明符中使用的不完整类型'claculator'

Ami*_*yan 1 c++ compiler-construction gcc

我开发了一个库,当我用GCC编译我的代码时(在带有CodeBlocks的Windows中),源代码无法编译并出现此错误:

错误:嵌套名称说明符中使用的不完整类型'claculator'.

我编写了一个完全生成此错误的示例代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个错误?

请注意我的源代码在Visual Studio中成功编译.

谢谢.

Naw*_*waz 7

它非常简单.定义test() 的定义calculator类为:

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);
}
Run Code Online (Sandbox Code Playgroud)

通过这种方式,将完整的定义calculator是已知的编译器时,它解析定义test().