gpt4 book ai didi

c++ - 为什么 'QTextStream(stdout) << endl;' 不编译,当 'QTextStream(stdout) << ""<< endl;'做?

转载 作者:太空狗 更新时间:2023-10-29 20:24:15 26 4
gpt4 key购买 nike

我一直在玩一些 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;
}

那么,为什么直接调用 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

请注意,发布的要点有效(输出换行符并退出),您需要更改周围的注释,以便以“编译失败”为前缀的行得到如下错误:

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

最佳答案

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

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

这通过非常量左值引用获取一个QTextStreamQTextStream(stdout) 是临时的,临时不能绑定(bind)到那些。

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

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

注意它是如何返回一个引用的(一旦完整的表达式完成,它就会挂起,但只要结果只用于链式运算符等就没问题)。此引用允许调用第一个提到的重载,而临时引用则不允许。

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

关于c++ - 为什么 'QTextStream(stdout) << endl;' 不编译,当 'QTextStream(stdout) << ""<< endl;'做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28972532/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com