我有一个 c++ 库,它提供了一个具有复杂逻辑的对象。在数据处理期间,这个对象向 std::cout 输出很多东西(现在是硬编码的)。我希望处理输出不要转到标准输出,而是转到 custm 小部件(一些文本显示)。我试图创建一个std::ostream
类成员,用一个参数设置它(std::cout 用于控制台应用程序和其他一些在 GUI 应用程序中处理的 ostream)。但是编译器向我抛出了以下错误:
[ 14%] 构建 CXX 对象 src/core/CMakeFiles/PietCore.dir/pvirtualmachine.cpp.o /usr/include/c++/4.6/ostream:在构造函数'PVirtualMachine::PVirtualMachine(QString)'中: /usr/include/c++/4.6/ostream:363:7: 错误: 'std::basic_ostream::basic_ostream() [with _CharT = char, _Traits = std::char_traits]' 受保护 /home/tomasz/Development/C++/piet/src/core/pvirtualmachine.cpp:33:50:错误:在此上下文中 在 /usr/include/c++/4.6/ios:45:0 包含的文件中, 来自 /usr/include/c++/4.6/ostream:40, 来自 /usr/include/c++/4.6/iterator:64, 来自/usr/include/qt4/QtCore/qlist.h:50, 来自/usr/include/qt4/QtCore/qvector.h:48, 来自/usr/include/qt4/QtGui/qpolygon.h:45, 来自 /usr/include/qt4/QtGui/qmatrix.h:45, 来自/usr/include/qt4/QtGui/qtransform.h:44, 来自/usr/include/qt4/QtGui/qimage.h:45, 来自/usr/include/qt4/QtGui/QImage:1, 来自/home/tomasz/Development/C++/piet/src/core/pcodepointer.h:17, 来自/home/tomasz/Development/C++/piet/src/core/pblockmanager.h:9, 来自/home/tomasz/Development/C++/piet/src/core/pvirtualmachine.h:10, 来自/home/tomasz/Development/C++/piet/src/core/pvirtualmachine.cpp:4: /usr/include/c++/4.6/bits/ios_base.h:在成员函数'std::basic_ios& std::basic_ios::operator=(const std::basic_ios&)'中: /usr/include/c++/4.6/bits/ios_base.h:791:5: 错误:'std::ios_base& std::ios_base::operator=(const std::ios_base&)' 是私有的 /usr/include/c++/4.6/bits/basic_ios.h:64:11:错误:在此上下文中 在 /usr/include/c++/4.6/iterator:64:0 包含的文件中, 来自/usr/include/qt4/QtCore/qlist.h:50, 来自/usr/include/qt4/QtCore/qvector.h:48, 来自/usr/include/qt4/QtGui/qpolygon.h:45, 来自 /usr/include/qt4/QtGui/qmatrix.h:45, 来自/usr/include/qt4/QtGui/qtransform.h:44, 来自/usr/include/qt4/QtGui/qimage.h:45, 来自/usr/include/qt4/QtGui/QImage:1, 来自/home/tomasz/Development/C++/piet/src/core/pcodepointer.h:17, 来自/home/tomasz/Development/C++/piet/src/core/pblockmanager.h:9, 来自/home/tomasz/Development/C++/piet/src/core/pvirtualmachine.h:10, 来自/home/tomasz/Development/C++/piet/src/core/pvirtualmachine.cpp:4: /usr/include/c++/4.6/ostream:在成员函数'std::basic_ostream& std::basic_ostream::operator=(const std::basic_ostream&)'中: /usr/include/c++/4.6/ostream:57:11: 注意:这里首先需要综合方法 'std::basic_ios& std::basic_ios::operator=(const std::basic_ios&)' /home/tomasz/Development/C++/piet/src/core/pvirtualmachine.cpp:在成员函数'void PVirtualMachine::setOutput(std::ostream)'中: /home/tomasz/Development/C++/piet/src/core/pvirtualmachine.cpp:216:11:注意:合成方法'std::basic_ostream& std::basic_ostream::operator=(const std::basic_ostream&)'首先需要这里
如果有人指出我出了什么问题,我会很高兴,因为我不知道......
我的代码如下所示:
- .h 文件
类 PVirtualMachine { 私人的: std::ostream 输出; [...] 上市: 无效 setOutput(std::ostream); [...] };
- .cpp 文件
无效 PVirtualMachine::setOutput(std::ostream os) { 输出=操作系统; }