【问题标题】:Visual Studio C++ ambigous call to overloaded functionVisual Studio C++ 对重载函数的模糊调用
【发布时间】:2011-12-23 16:34:12
【问题描述】:

编辑:我发布了整个班级(删除了一些与错误无关的内容)

我做了以下课程:

class packet
{public:char *  buffer;
    int     size;
    int     data;

    packet();
    packet(packet &text, int length=-1);
    packet(char * text, int length=-1);
    packet(int val);
    packet(char c);
    packet(double d);
    packet(float f);

    ~packet();

    packet &    operator=       (packet &text);
    packet      operator+       (packet &text);
    packet &    operator+=      (packet &text);
    packet &    operator|=      (packet &text);
    bool        operator==      (packet &text);
    bool        operator*=      (packet &text);
    bool        operator!=      (packet &text);

                operator char*  () const;
                operator int    () const;
                operator float  () const;
    char        operator []     (int pos)  const;
};

我使用这样的类:

    packet p = packet();

或

return packet();

Visual Studio 给了我这个错误:

test.cpp(162): error C2668: 'packet::packet' : ambiguous call to overloaded function
...packet.h(26): could be 'packet::packet(float)'
...packet.h(23): or       'packet::packet(int)'
...packet.h(22): or       'packet::packet(char *,int)'

有谁知道我在这里做错了什么?为什么这是模棱两可的?

PS:我认为这与底部的 4 个运算符有关,但我对重载这些运算符有点模糊......

解决方案:我通过将一些构造函数标记为显式来使其工作:

class packet
{public:char *  buffer;
    int     size;
    int     data;

    packet();
    packet(packet &text, int length=-1);
    explicit packet(char * text, int length=-1);
    explicit packet(int val);
    explicit packet(char c);
    explicit packet(double d);
    explicit packet(float f);

    ~packet();

    packet &    operator=       (packet &text);
    packet      operator+       (packet &text);
    packet &    operator+=      (packet &text);
    packet &    operator|=      (packet &text);
    bool        operator==      (packet &text);
    bool        operator*=      (packet &text);
    bool        operator!=      (packet &text);

                operator char*  () const;
                operator int    () const;
                operator float  () const;
    char        operator []     (int pos)  const;
};

【问题讨论】:

  • 你确定这是你的全部代码吗?
  • 我确定这不是整个代码。我的猜测是他将0 或NULL 传递给构造函数,例如packet(0) 或packet(NULL)。
  • 我很确定这家伙的可选参数比实际代码中的要多。
  • 你说packet(0)?
  • @Luchian:显然不可能,因为packet() 不能含糊。

标签: c++ visual-studio-2010 inheritance operators


【解决方案1】:

如果错误确实发生在您尝试将函数结果分配给新变量的地方,则问题可能出在您的复制构造函数上。您应该在const 中创建packet&,以便它可以与临时对象一起使用:

packet(const packet & text, int length=-1);

如果您的类可以隐式转换为int、float、....,则其他构造函数可能会在这种情况下发挥作用。

由于此类问题,通常建议不要添加不必要的转换运算符并将构造函数标记为 explicit 以避免意外的隐式转换。

【讨论】:

  • 非常感谢,这帮助我解决了我的问题。我将几个构造函数标记为显式,它编译并工作!
  • 请注意,如果转换运算符被排除在外(部分原因是 MSVC 会做一些非标准的事情),MSVC 2010 会在 /W4 上给出一个很好的信息:C:\temp\test.cpp(35) : warning C4239: nonstandard extension used : 'argument' : conversion from 'packet' to 'packet &', A non-const reference may only be bound to an lvalue; copy constructor takes a reference to non-const跨度>
  • @HardCoder:将许多packet& 参数标记为const 可能是个好主意。
猜你喜欢
  • 2011-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 2014-09-05
  • 2016-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多