我刚开始学习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/