读书人

call of overloaded amp;#x27;T()amp;#x27;

发布时间: 2013-08-16 14:29:57 作者: rapoo

call of overloaded 'T()' is ambiguou。
头文件:
#include<string>
using std::string;

class T    //基类
{
private:
string f;
string l;
bool h;
public:
T();
T(const string &fn="none",const string &ln="none",bool ht=false);
...
}
class R:public T
{
private:
int r;
public:
R(int ra,const string &fn,const string &ln,bool ht);
...
}

T::T()
{
f="okok";
l="koko";
h=0;
}

T::T(const string &fn,const string &ln,bool ht)
{
f=fn;
l=ln;
h=ht;
}
...
R::R(int ra,const string &fn,const string &ln,bool ht)//省略成员初始化,程序默认基类构造函数,问题就在这里。
{
r=ra;
}
...



由于35行代码省略了 成员初始化 所以 编译的时候会提示二义性 call of overloaded 'T()' is ambiguou。
我觉得如果省略了初始化再加上我已经显式声明了R::R()就应该调用成员函数R::R();
可是编译器却提示二义性 原因可能是调用R::R()和R::R(const std::string&, const std::string&, bool);
为什么会造成二义性呢。 类 String
[解决办法]
T();
T(const string &fn="none",const string &ln="none",bool ht=false);

第二个为每个参数都指定默认值,那就就是默认的构造函数
调用T(),2个函数都符合要求,当然是二义性。
[解决办法]
重载函数如果参数有默认值,就要注意省略默认参数后会不会和已有函数冲突

读书人网 >C++

热点推荐