小编ap2*_*014的帖子

C++默认构造函数

我使用以下代码得到编译错误:

main.cpp: In function âint main()â:
main.cpp:38: error: no matching function for call to âComplex::Complex(Complex)â
main.cpp:22: note: candidates are: Complex::Complex(Complex&)
main.cpp:15: note:                 Complex::Complex(double, double)
Run Code Online (Sandbox Code Playgroud)

但是当我将复制构造函数的参数类型更改为const Complex&时,它可以工作.我认为默认构造函数将使用2 Complex :: Complex(2.0,0.0)调用,然后将调用复制构造函数来创建一个带有Complex(2.0.0)的副本.不正确吗?

#include <iostream>
using namespace std;

class Complex {
        double re;
        double im;

public:
        Complex(double re=0, double im=0);
        Complex(Complex& c);
        ~Complex() {};
        void print();
};

Complex::Complex(double re, double im)
{
        cout << "Constructor called with " << re << " " << im << endl;
        this->re = re;
        this->im = im;
} …
Run Code Online (Sandbox Code Playgroud)

c++ constructor

3
推荐指数
1
解决办法
1443
查看次数

标签 统计

c++ ×1

constructor ×1