c++ - 错误: Qualified reference to 'Complex' is a constructor name rather than a type in this context

标签 c++ class constructor

我刚开始学习C++,我不明白为什么当我在类外定义Complex类的构造函数时,会出现编译错误,请帮忙!


using namespace std;

int main(int argc, const char * argv[]) {
    
    class Complex{
    private:
        double real;
        double imag;
    public:
        Complex (double r, double i);
    };
    
    Complex::Complex(double r, double i){
        real = r;
        imag = i;
    };
    

    return 0;
};

最佳答案

您应该将 Complex 类的定义移至 外部 main() 函数,如下所示:

#include <iostream>
class Complex{
    private:
        double real;
        double imag;
    public:
        Complex (double r, double i);
    };
    
    Complex::Complex(double r, double i){
        real = r;
        imag = i;
    };


int main(int argc, const char * argv[]) {

    return 0;
}; //unnecessary semicolon. You should remove this semicolon too

关于c++ - 错误: Qualified reference to 'Complex' is a constructor name rather than a type in this context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70783769/

相关文章:

c++ - 如何根据一本书的段落创建思维导图

c++ - 非类函数可以私有(private)化吗?

c++ - c++中继承的构造函数

python - 使用 Python `inspect` 模块列出所有类成员

python - 正在定义的类型对象的类型提示

c++ - 默认构造函数是在什么情况下生成的?

c++ - 使用boost asio的线程池

javascript - Javascript 类可以使用揭示模块模式来实现吗?

c++ - 如何在 C++ 中同时使用默认和自定义复制构造函数?

javascript - MouseEventConstructor 不是构造函数