编辑ambigous来电:我张贴全班同学(条纹数的误差不相干的东西了)Visual Studio的C++来重载函数
我做了下面的类:
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;
};
你确定这是你的整个代码? – 2011-12-23 16:36:28
我确定它不是整个代码。我的猜测是,他正在传递'0'或'NULL'给构造函数,像'packet(0)'或'packet(NULL)'。 – Nawaz 2011-12-23 16:37:05
显示实际的代码。 – 2011-12-23 16:37:18