Overloaded 'QString (int) is ambiguous

The following code fragment gives a compilation error call of overloaded 'QString(int)' is ambiguous with qt 4.7.3 (Linux 64bit system, debian is 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?

+4
source share
2 answers

The corresponding bit is the line:

 QSAConnection() : sender(0), signal(0), function_ref() { } 

Since signal is a QString , the signal(0) bit attempts to call the constructor in the QString class, which takes an integer as the only parameter. QString does not have such a constructor according to the Qt documentation. However, it has a constructor that takes char and a QChar , which has an implicit conversion from int . I expect this to be ambiguous between the two.

Did you mean this instead?

 QSAConnection() : sender(0), signal(), function_ref() { } 

which will be initialized by default to signal . Note that there is technically no need to include it in the initialization list in general in this case.

+5
source

It's good. If this was not ambiguous, you can throw a null pointer exception on QString::QString(char const* src) . This is a common error with std::string .

0
source

All Articles