explicite类型转换不适用于重载的构造函数

时间:2015-12-07 09:31:54

标签: c++ constructor overloading

我最近从VS6切换到VS-Express 2013.转换旧代码当然会给我带来很多警告和警告,但有一件事我真的很喜欢:

为什么我在这里收到错误?

当我尝试编译时

 ParseValue op_ParseValue()
    {
        switch (m_dt)
        {
        case NUM:
            return ParseValue( (int) Value);
            break;
        case P_STRING:
            return ParseValue((const char*)Symbol_Name);
            break;
        case P_LITERAL:
            return ParseValue(Symbol_Name);
            break;
        case P_ERROR:
            return ParseValue(P_ERROR);
            break;
        }
        return ParseValue(P_ERROR);
    };

我收到编译器错误C2668:ambiguent调用重载函数。

我发现的唯一提示是消除此错误是为了进行明确的类型转换,但我已经这样做了。实际上我看不出需要任何类型转换,因为所有参数都非常适合构造函数参数。

这是类定义(部分)

class ParseValue
    {
    public:
        ParseValue(ParseValue &pv) :_charval(""), _intval(pv._intval), _type(pv._type)
        {
            if (_type == P_LITERAL || _type == P_STRING || _type == P_REF)
            {
                _charval = pv._charval;
            }
        }
        ParseValue(DataType dt) :_charval(""), _intval(0), _type(dt)
        {
        }
        ParseValue(int num) :_charval(""), _intval(num), _type(NUM)
        {
        }

        ParseValue(TCSymbolReference &ref) :_charval(""), _intval(0), _type(P_REF)
        {
            _charval = ref.Name();
        }

        ParseValue(const char *str) :_charval(""), _intval(0), _type(P_LITERAL)
        {
            _charval = str;
        }
        ~ParseValue()
        {
        }
        ParseValue operator ==(ParseValue &other);

        ParseValue operator !=(ParseValue &other);

        ParseValue operator *(ParseValue &other);

        ParseValue operator /(ParseValue &other);
        ParseValue operator %(ParseValue &other);

        ParseValue operator +(ParseValue &other);
        ParseValue operator -(ParseValue &other);
        ParseValue operator &&(ParseValue &other);
        ParseValue operator !();
        ParseValue operator ||(ParseValue &other);
        ParseValue operator &(ParseValue &other);
        ParseValue operator |(ParseValue &other);
        ParseValue operator ^(ParseValue &other);

        ParseValue operator <(ParseValue &other);
        ParseValue operator >(ParseValue &other);
        ParseValue operator =(ParseValue &other);
        ParseValue operator =(int num)
        {
            _intval = num;
            _type = NUM;
            return *this;
        }
        operator int();
        operator char* ();
        operator void* ();
        int operator--();
        int operator++();
 }

这是编译错误(抱歉是德语)

1>c:\users\mkilianj\documents\testimprovement\bidl\parsetree1.h(225): error C2668: 'ParseTree::ParseValue::ParseValue': Mehrdeutiger Aufruf einer überladenen Funktion
1>          c:\users\mkilianj\documents\testimprovement\bidl\parsetree1.h(56): kann 'ParseTree::ParseValue::ParseValue(const char *)' sein
1>          c:\users\mkilianj\documents\testimprovement\bidl\parsetree1.h(51): oder "ParseTree::ParseValue::ParseValue(TCSymbolReference &)"
1>          c:\users\mkilianj\documents\testimprovement\bidl\parsetree1.h(47): oder "ParseTree::ParseValue::ParseValue(int)"
1>          c:\users\mkilianj\documents\testimprovement\bidl\parsetree1.h(44): oder "ParseTree::ParseValue::ParseValue(ParseTree::DataType)"
1>          c:\users\mkilianj\documents\testimprovement\bidl\parsetree1.h(37): oder "ParseTree::ParseValue::ParseValue(ParseTree::ParseValue &)"

1 个答案:

答案 0 :(得分:0)

我想你需要在你的拷贝构造函数中添加const:

ParseValue(const ParseValue &pv)
           ^^^^^ ~~~ !

您应该始终使您的复制构造函数采用const引用参数。否则,如果你不需要const - 那么使用move构造函数。

错误的原因是您从函数返回值,因此编译器会创建一个临时值,该临时值将复制到您在函数调用位置中指定的任何变量 - 使用复制构造函数。编译错误的原因是,编译器创建的临时对象不能绑定到非const引用。在您的情况下,编译器会找到适合进行转换的转换运算符(您提供适当的构造函数),但由于它们不明确,您会得到错误。

相关问题