重载“QString(int)”含糊不清
下面的代码片段给出了编译错误 使用 qt 4.7.3 调用重载的“QString(int)”不明确
(系统是linux 64位,debian不稳定)
struct QSAConnection
{
QSAConnection() : sender(0), signal(0), function_ref() { }
QSAConnection(QObject *send, const char *sig, QSObject ref)
: sender(send), signal(QLatin1String(sig)), function_ref(ref) { }
QObject *sender;
QString signal;
QSObject function_ref;
};
有什么提示吗?
The following code fragment gives the compilation errorcall of overloaded ‘QString(int)’ is ambiguous
with qt 4.7.3
(system is linux 64bit, debian unstable)
struct QSAConnection
{
QSAConnection() : sender(0), signal(0), function_ref() { }
QSAConnection(QObject *send, const char *sig, QSObject ref)
: sender(send), signal(QLatin1String(sig)), function_ref(ref) { }
QObject *sender;
QString signal;
QSObject function_ref;
};
Any tips?
相关位是这一行:
由于
signal
是一个QString
,因此signal(0)
位尝试调用上的构造函数>QString
类,将整数作为其唯一参数。根据 Qt 文档,QString
没有这样的构造函数。不过,它确实有一个采用char
的构造函数,以及一个从int
进行隐式转换的QChar
。我预计这两者之间是模棱两可的。你的意思是这个吗?
这将默认初始化
信号
。请注意,从技术上讲,在这种情况下根本没有必要将其包含在初始值设定项列表中。The relevant bit is this line:
Since
signal
is aQString
, thesignal(0)
bit is trying to call a constructor on theQString
class that takes an integer as its only parameter.QString
has no such constructor according to the Qt documentation. It does however have a constructor taking achar
, and aQChar
which has an implicit conversion fromint
. I expect it's ambiguous between those two.Did you mean this instead?
Which will default initialize
signal
. Note that technically it's not necessary to include it in the initializer list at all in that case.