注册 登录
编程论坛 C++教室

vs2015对于重载对于重载函数调用不明确的困惑

zerahfan 发布于 2017-06-07 14:26, 3810 次点击
程序代码:
#include<iostream>
using namespace std;


class CCopmlex
{
private:
    double m_real, m_unreal;//实数,虚数
public:
    CCopmlex();
    CCopmlex(double r = 0, double u = 0);
    CCopmlex operator+(const CCopmlex &t);
    friend void print(const CCopmlex &t);
};
CCopmlex::CCopmlex()
{
    m_real = 0;
    m_unreal = 0;
}
CCopmlex::CCopmlex(double r, double u)
{
    m_real = r;
    m_unreal = u;
}
inline CCopmlex CCopmlex::operator+(const CCopmlex &t)//重载运算符
{
    return CCopmlex(m_real + t.m_real, m_unreal + t.m_unreal);

}


void  print(const CCopmlex &t)
{
    if (t.m_unreal < 0)
        cout << t.m_real << t.m_unreal << "i" << endl;
    cout << t.m_real << "+" << t.m_unreal << "i" << endl;
}

int main()
{
    CCopmlex p1(2.0, 3.0), p2(4.0, 2.3);
    CCopmlex p3;
    p3 = p1 + p2;
    cout << "the answer is";
    print(p3);

    return 0;
}

总是提示类包含多个默认构造函数;对重载函数调用不明确
2 回复
#2
rjsp2017-06-07 14:54
要记得贴编译器给出的错误信息
CCopmlex p3;
error C2668: 'CCopmlex::CCopmlex' : ambiguous call to overloaded function
             could be 'CCopmlex::CCopmlex(double,double)'
             or       'CCopmlex::CCopmlex(void)'

#3
某一天2017-06-09 15:06
    CCopmlex();
    CCopmlex(double r = 0, double u = 0);
以上这两个构造函数矛盾了,
比如当系统调用CCopmlex()时,第一个函数可以被调用,然而第二个函数参数列表都有默认值也可以调用,系统就不知道要调用哪个了,因此报错.

解决办法就是吧CCopmlex()这个构造函数删除掉
1