Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ GCC错误:嵌套名称说明符中使用的类型“claculator”不完整_C++_Gcc_Compiler Construction - Fatal编程技术网

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

C++ GCC错误:嵌套名称说明符中使用的类型“claculator”不完整,c++,gcc,compiler-construction,C++,Gcc,Compiler Construction,我开发了一个库,当我在带有代码块的Windows中使用GCC编译代码时,源代码未编译,出现以下错误: 错误:嵌套名称说明符中使用的类型“claculator”不完整 我编写了一个生成此错误的示例代码: class claculator; template<class T> class my_class { public: void test() { // GCC error: incomplete type 'claculator' use

我开发了一个库,当我在带有代码块的Windows中使用GCC编译代码时,源代码未编译,出现以下错误:

错误:嵌套名称说明符中使用的类型“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;
}
如何解决此错误

请注意,我的源代码在VisualStudio中编译成功

谢谢。

很简单。将计算器类定义后的测试定义为:

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);
}
这样,编译器在解析测试定义时就知道计算器的完整定义。

非常简单。将计算器类定义后的测试定义为:

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);
}

这样,编译器在解析测试定义时就知道计算器的完整定义。

为什么不可能移动计算器声明而不是定义!在模板之前?使用“claculator”而不是“calculator”有什么特殊原因吗?还有,为什么不能将my_类的定义移到claculator下面?这听起来像是一个家庭作业要求…@Jonathan:对不起,拼写小姐:D这不是我的实际源代码,这个源代码准确地生成了错误,我的实际源代码太复杂,无法移动类。例如,我在我的_类中使用计算器,也在计算器中使用我的_类。您的程序格式不正确。如果将my_类设置为非模板类,所有编译器都会抱怨,因为对于不完整的类型claculator,您无法访问claculator::add。模板也是如此,但有些编译器(如VC++)对模板的处理有些放松。他们错误地将检查推迟到实例化时间。他们真的不应该这样做,标准对此相当明确。@n.m:这是一个示例代码,我编写它是为了生成错误,这不是一个真正的源代码,而是生成相同的错误。为什么不可能移动claculator声明而不是定义!在模板之前?使用“claculator”而不是“calculator”有什么特殊原因吗?还有,为什么不能将my_类的定义移到claculator下面?这听起来像是一个家庭作业要求…@Jonathan:对不起,拼写小姐:D这不是我的实际源代码,这个源代码准确地生成了错误,我的实际源代码太复杂,无法移动类。例如,我在我的_类中使用计算器,也在计算器中使用我的_类。您的程序格式不正确。如果将my_类设置为非模板类,所有编译器都会抱怨,因为对于不完整的类型claculator,您无法访问claculator::add。模板也是如此,但有些编译器(如VC++)对模板的处理有些放松。他们错误地将检查推迟到实例化时间。他们真的不应该这样做,标准对此相当明确。@n.m:这是一个示例代码,我编写它是为了生成错误,这不是真正的源代码,而是生成相同的错误。小注。我想要一个计算器。。。不是计算器:@Nawaz:这个想法可能在更真实的示例中不起作用,因为模板实现通常必须与定义位于同一个头中。@iammilind:我认为这是他的代码中的拼写错误。他可能想像我一样拼写正确。@Nawaz:让我解释一下我的观点。OP坚持my_类的定义先于claculator的定义,也就是说,claculator.h包括my_类.h。但是,整个模板必须在my_class.h中。那会再次造成问题。@Nawaz:是的,包括在声明之后才是解决办法。小注。我想要一个计算器。。。不是计算器:@Nawaz:这个想法可能在更真实的示例中不起作用,因为模板实现通常必须与定义位于同一个头中。@iammilind:我认为这是他的代码中的拼写错误。他可能想像我一样拼写正确。@Nawaz:让我解释一下我的观点。OP坚持my_类的定义先于claculator的定义,也就是说,claculator.h包括my_类.h。但是,整个模板必须在my_class.h中。那会再次造成问题。@Nawaz:是的,包括在声明之后才是解决办法。