为什么没有' QTextStream(stdout)<< ENDL;'编译,当' QTextStream(stdout)<< "" << ENDL;'呢?

时间:2015-03-10 19:33:17

标签: c++ qt

我一直在玩一些c ++你好世界的东西,并且认为我会得到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;
}

那么,为什么对QString(out) << end;的裸调用无法编译?编译器指出没有转换的情况,但为什么它正确转换为空字符串?这是qt中的错误,还是c ++如何查找模板?完全编译错误如下。

有一个要点,为了测试:

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

请注意,作为已发布的gist工作(输出换行符并退出),您需要更改注释,以便前缀为“无法编译”的行以获得如下错误:

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

1 个答案:

答案 0 :(得分:6)

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

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

这需要QTextStream非常量左值引用。 QTextStream(stdout)是暂时的,临时的不能与那些绑定。

还有const char *的成员函数重载:

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

注意这是如何返回一个引用(一旦完整表达式完成就会悬挂,但只要结果仅在链式运算符中使用等,就会很好)。此引用允许调用第一个提到的重载,而临时不会。

就个人而言,我会选择你的最后一个选项,除非有更好的方式我不知道(我还没有使用过Qt)。此选项类似于标准对std::cout的处理方式,但您不是全局变量,而是定义它。