您好,我目前正在尝试初始化一个类中的复数
class complex_class{
public:
complex<double> mycomplex;
complex_class(double real, double img){
//mycomplex(real, img);
mycomplex.real(real);
mycomplex.imag(img);
}
};当我尝试直接赋值时,出现了一条错误消息
错误:类型'complex‘没有提供调用运算符mycomplex(real,img);
它只适用于函数real和imag
所以我想问你们,我做错了什么,我不理解什么。
感谢您的阅读和帮助
发布于 2019-10-23 20:51:49
成员变量是在调用构造函数体之前构造和初始化的。
如果要将成员变量作为对象初始化的一部分进行初始化,则需要在调用构造函数正文之前执行此操作,为此需要使用构造函数初始化器列表。
就像这样
complex_class(double real, double img)
: mycomplex(real, img) // Initialize the mycomplex object through its constructor
{
// Empty, as mycomplex already is initialized
}https://stackoverflow.com/questions/58523083
复制相似问题