为什么 'QTextStream(stdout) << endl;' 不能编译,而 'QTextStream(stdout) << "" << endl;' 可以呢?

3

我一直在尝试使用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 functionint main(int, char**)’:
qt.cpp:7:23: error: ambiguous overload foroperator<<’ (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&)’ toconst void*’
make: *** [qt.o] Error 1
1个回答

6

endl 这样的函数有一个过载:

QTextStream &operator<<(QTextStream &s, QTextStreamFunction f);

这里使用了一个非常量左值引用的QTextStream。而QTextStream(stdout)是一个临时值,而临时值无法绑定到非常量引用。

还有一个成员函数重载,针对const char *:

QTextStream &operator<<(const char *c);

请注意,这将返回一个引用(一旦完整表达式完成,它将悬挂,但只要结果仅在链接操作符等中使用,就可以正常工作)。该引用允许调用先前提到的重载,而临时对象则不行。
个人而言,除非有更好的方法我不知道(我没有使用Qt),否则我会选择您的最后一个选项。此选项类似于标准使用std :: cout的方式,但它是由您定义的,而不是全局变量。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接