首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么'QTextStream(stdout) << endl;‘编译,而'QTextStream(stdout) <<’则不编译‘<< endl;’?

为什么'QTextStream(stdout) << endl;‘编译,而'QTextStream(stdout) <<’则不编译‘<< endl;’?
EN

Stack Overflow用户
提问于 2015-03-10 19:33:16
回答 1查看 1.7K关注 0票数 3

我一直在玩一些c++你好世界的东西,并认为我会得到一个旋转的qt字符串。回传和转换字符串是很好的,但是endl似乎很特殊:

代码语言:javascript
复制
#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中的一个bug,还是c++查找模板的方式?下面是完整的编译器错误。

有一个要点,为了测试:

代码语言:javascript
复制
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工作(输出换行符和出口),您需要更改周围的注释,以便以“失败编译”为前缀的行获得如下错误:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-10 20:07:50

endl这样的函数存在过载

代码语言:javascript
复制
QTextStream &operator<<(QTextStream &s, QTextStreamFunction f);

这将通过非const引用获取QTextStreamQTextStream(stdout)是暂时的,临时人员不能绑定到这些。

const char *也有一个成员函数重载

代码语言:javascript
复制
QTextStream &operator<<(const char *c);

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

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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28972532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档