我一直在尝试使用C++编写Hello World程序,并尝试使用Qt字符串。输出和转换字符串都不错,但endl
似乎很特殊:
#include <QTextStream>
int main(int, char*[])
{
// Works, but is hacky (note: empty string):
// QTextStream(stdout) << "" << endl;
// Does not compile:
// QTextStream(stdout) << endl;
// Best? way (thanks to suy on #qt@freenode):
QTextStream out(stdout);
out << endl;
return 0;
}
因此,为什么对的裸调用无法编译?编译器指示这种情况没有转换,但为什么空字符串可以正确转换?这是qt中的一个错误还是c ++查找模板的方式?以下是完整的编译器错误。
有一个gist,并且为了测试:
mkdir qt
cd qt
wget https://gist.github.com/e12e/f79e9f0e66ee3600e0aa/raw/7ab8239060134601e93a0013706df638af0b3edf/qt.cpp
cat qt.cpp #Check what you're compiling ;-)
qmake -project
qmake -makefile
make
./qt # outputs empty line
请注意,作为发布的要点是有效的(输出一个换行符并退出),您需要更改周围的注释,以便以“无法编译”为前缀的行会出现错误,例如:
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -I. -I. -isystem /usr/include/x86_64-linux-gnu/qt5 -isystem /usr/include/x86_64-linux-gnu/qt5/QtGui -isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -I. -o qt.o qt.cpp
qt.cpp: In function ‘int main(int, char**)’:
qt.cpp:7:23: error: ambiguous overload for ‘operator<<’ (operand types are ‘QTextStream’ and ‘QTextStream&(QTextStream&)’)
QTextStream(stdout) << endl;
^
qt.cpp:7:23: note: candidates are:
In file included from /usr/include/x86_64-linux-gnu/qt5/QtCore/QTextStream:1:0,
from qt.cpp:1:
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:175:18: note: QTextStream& QTextStream::operator<<(QChar) <near match>
QTextStream &operator<<(QChar ch);
^
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:175:18: note: no known conversion for argument 1 from ‘QTextStream&(QTextStream&)’ to ‘QChar’
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:176:18: note: QTextStream& QTextStream::operator<<(char) <near match>
QTextStream &operator<<(char ch);
^
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:176:18: note: no known conversion for argument 1 from ‘QTextStream&(QTextStream&)’ to ‘char’
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:177:18: note: QTextStream& QTextStream::operator<<(short int) <near match>
QTextStream &operator<<(signed short i);
^
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:177:18: note: no known conversion for argument 1 from ‘QTextStream&(QTextStream&)’ to ‘short int’
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:178:18: note: QTextStream& QTextStream::operator<<(short unsigned int) <near match>
QTextStream &operator<<(unsigned short i);
^
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:178:18: note: no known conversion for argument 1 from ‘QTextStream&(QTextStream&)’ to ‘short unsigned int’
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:179:18: note: QTextStream& QTextStream::operator<<(int) <near match>
QTextStream &operator<<(signed int i);
^
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:179:18: note: no known conversion for argument 1 from ‘QTextStream&(QTextStream&)’ to ‘int’
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:180:18: note: QTextStream& QTextStream::operator<<(unsigned int) <near match>
QTextStream &operator<<(unsigned int i);
^
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:180:18: note: no known conversion for argument 1 from ‘QTextStream&(QTextStream&)’ to ‘unsigned int’
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:181:18: note: QTextStream& QTextStream::operator<<(long int) <near match>
QTextStream &operator<<(signed long i);
^
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:181:18: note: no known conversion for argument 1 from ‘QTextStream&(QTextStream&)’ to ‘long int’
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:182:18: note: QTextStream& QTextStream::operator<<(long unsigned int) <near match>
QTextStream &operator<<(unsigned long i);
^
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:182:18: note: no known conversion for argument 1 from ‘QTextStream&(QTextStream&)’ to ‘long unsigned int’
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:183:18: note: QTextStream& QTextStream::operator<<(qlonglong) <near match>
QTextStream &operator<<(qlonglong i);
^
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:183:18: note: no known conversion for argument 1 from ‘QTextStream&(QTextStream&)’ to ‘qlonglong {aka long long int}’
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:184:18: note: QTextStream& QTextStream::operator<<(qulonglong) <near match>
QTextStream &operator<<(qulonglong i);
^
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:184:18: note: no known conversion for argument 1 from ‘QTextStream&(QTextStream&)’ to ‘qulonglong {aka long long unsigned int}’
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:191:18: note: QTextStream& QTextStream::operator<<(const void*) <near match>
QTextStream &operator<<(const void *ptr);
^
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtextstream.h:191:18: note: no known conversion for argument 1 from ‘QTextStream&(QTextStream&)’ to ‘const void*’
make: *** [qt.o] Error 1