无法使用 ostream << 字符串流重载 参数 1 没有已知的转换



我试图使用&lt;&lt;&lt;对于Ostream GCC失败:

错误:"操作员&lt;&lt;''无匹配(操作数类型是'std :: stringstream&amp;(){aka std :: __ cxx11 :: basic_stringstream&amp;()}}'and'const test')

据我所知,定义了操作员,我使用stringstream(输入运算符(用于IStream过载的其他类的输入操作员),使用相同的编译器的任何问题。

示例代码:

// Header
#include <istream>
#include <ostream>
class Test {
public:
    Test* Clone() const { return onClone(); }
    friend std::istream& operator >> (std::istream& lhs, Test& rhs) { rhs.onLoad(lhs); return lhs; }
    friend std::ostream& operator << (std::ostream& lhs, const Test& rhs) { rhs.onSave(lhs); return lhs; }
private:
    virtual void onLoad(std::istream& in) {}
    virtual void onSave(std::ostream& out) const {}
    Test* Test::onClone() const;
};
// Source
#include <sstream>
Test* Test::onClone() const
{
    Test * tmp = new Test();
    std::stringstream& buf();
    buf << (*this); //!< ERROR HERE
    buf >> (*tmp);
    return tmp;
}

过滤的构建输出:

g++.exe -Wzero-as-null-pointer-constant -std=c++14 -m64 -Wcast-align -Wundef -Wfloat-equal -Winline -Wunreachable-code -Wswitch-enum -Wswitch-default -Weffc++ -Wzero-as-null-pointer-constant -pg -g -D_WIN32 -D_UNICODE  -Iinclude -c test.cpp -o objDebugtest.o
test.cpp: In member function 'Test* Test::onClone() const':
test.cpp:23:9: error: no match for 'operator<<' (operand types are 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}' and 'const Test')
     buf << (*this); //!< ERROR HERE
         ^
In file included from %LibaryPath%/istream:39:0,
                 from test.cpp:2:
%LibaryPath%/ostream:628:5: note: candidate: template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)
     operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
     ^
%LibaryPath%/ostream:628:5: note:   template argument deduction/substitution failed:
test.cpp:23:18: note:   mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf << (*this); //!< ERROR HERE
                  ^
In file included from %LibaryPath%/istream:39:0,
                 from test.cpp:2:
%LibaryPath%/ostream:574:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
     operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
     ^
%LibaryPath%/ostream:574:5: note:   template argument deduction/substitution failed:
test.cpp:23:18: note:   mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf << (*this); //!< ERROR HERE
                  ^
In file included from %LibaryPath%/istream:39:0,
                 from test.cpp:2:
%LibaryPath%/ostream:569:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
     operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
     ^
%LibaryPath%/ostream:569:5: note:   template argument deduction/substitution failed:
test.cpp:23:18: note:   mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf << (*this); //!< ERROR HERE
                  ^
In file included from %LibaryPath%/istream:39:0,
                 from test.cpp:2:
%LibaryPath%/ostream:556:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
     operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
     ^
%LibaryPath%/ostream:556:5: note:   template argument deduction/substitution failed:
test.cpp:23:18: note:   mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf << (*this); //!< ERROR HERE
                  ^
In file included from %LibaryPath%/ostream:638:0,
                 from %LibaryPath%/istream:39,
                 from test.cpp:2:
%LibaryPath%/bits/ostream.tcc:321:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
     operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
     ^
%LibaryPath%/bits/ostream.tcc:321:5: note:   template argument deduction/substitution failed:
test.cpp:23:18: note:   mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf << (*this); //!< ERROR HERE
                  ^
In file included from %LibaryPath%/istream:39:0,
                 from test.cpp:2:
%LibaryPath%/ostream:539:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
     operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
     ^
%LibaryPath%/ostream:539:5: note:   template argument deduction/substitution failed:
test.cpp:23:18: note:   mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf << (*this); //!< ERROR HERE
                  ^
In file included from %LibaryPath%/istream:39:0,
                 from test.cpp:2:
%LibaryPath%/ostream:519:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
     operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
     ^
%LibaryPath%/ostream:519:5: note:   template argument deduction/substitution failed:
test.cpp:23:18: note:   mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf << (*this); //!< ERROR HERE
                  ^
In file included from %LibaryPath%/istream:39:0,
                 from test.cpp:2:
%LibaryPath%/ostream:514:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
     operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
     ^
%LibaryPath%/ostream:514:5: note:   template argument deduction/substitution failed:
test.cpp:23:18: note:   mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf << (*this); //!< ERROR HERE
                  ^
In file included from %LibaryPath%/istream:39:0,
                 from test.cpp:2:
%LibaryPath%/ostream:508:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
     operator<<(basic_ostream<char, _Traits>& __out, char __c)
     ^
%LibaryPath%/ostream:508:5: note:   template argument deduction/substitution failed:
test.cpp:23:18: note:   mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf << (*this); //!< ERROR HERE
                  ^
In file included from %LibaryPath%/istream:39:0,
                 from test.cpp:2:
%LibaryPath%/ostream:502:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
     operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
     ^
%LibaryPath%/ostream:502:5: note:   template argument deduction/substitution failed:
test.cpp:23:18: note:   mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf << (*this); //!< ERROR HERE
                  ^
In file included from %LibaryPath%/istream:39:0,
                 from test.cpp:2:
%LibaryPath%/ostream:497:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
     operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
     ^
%LibaryPath%/ostream:497:5: note:   template argument deduction/substitution failed:
test.cpp:23:18: note:   mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf << (*this); //!< ERROR HERE
                  ^
In file included from %LibaryPath%/bits/ios_base.h:46:0,
                 from %LibaryPath%/ios:42,
                 from %LibaryPath%/istream:38,
                 from test.cpp:2:
%LibaryPath%/system_error:209:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::error_code&)
     operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
     ^
%LibaryPath%/system_error:209:5: note:   template argument deduction/substitution failed:
test.cpp:23:18: note:   mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf << (*this); //!< ERROR HERE
                  ^
In file included from %LibaryPath%/string:52:0,
                 from %LibaryPath%/bits/locale_classes.h:40,
                 from %LibaryPath%/bits/ios_base.h:41,
                 from %LibaryPath%/ios:42,
                 from %LibaryPath%/istream:38,
                 from test.cpp:2:
%LibaryPath%/bits/basic_string.h:5167:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
     operator<<(basic_ostream<_CharT, _Traits>& __os,
     ^
%LibaryPath%/bits/basic_string.h:5167:5: note:   template argument deduction/substitution failed:
test.cpp:23:18: note:   mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf << (*this); //!< ERROR HERE
                  ^
test.cpp:9:26: note: candidate: std::ostream& operator<<(std::ostream&, const Test&)
     friend std::ostream& operator << (std::ostream& lhs, const Test& rhs) { rhs.onSave(lhs); return lhs; }
                          ^
test.cpp:9:26: note:   no known conversion for argument 1 from 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}' to 'std::ostream& {aka std::basic_ostream<char>&}'
test.cpp:24:9: error: no match for 'operator>>' (operand types are 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}' and 'Test')
     buf >> (*tmp);
         ^
In file included from test.cpp:2:0:
%LibaryPath%/istream:924:5: note: candidate: template<class _CharT, class _Traits, class _Tp> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&&, _Tp&)
     operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
     ^
%LibaryPath%/istream:924:5: note:   template argument deduction/substitution failed:
test.cpp:24:17: note:   mismatched types 'std::basic_istream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf >> (*tmp);
                 ^
In file included from test.cpp:2:0:
%LibaryPath%/istream:808:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)
     operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
     ^
%LibaryPath%/istream:808:5: note:   template argument deduction/substitution failed:
test.cpp:24:17: note:   mismatched types 'std::basic_istream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf >> (*tmp);
                 ^
In file included from test.cpp:2:0:
%LibaryPath%/istream:803:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
     operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
     ^
%LibaryPath%/istream:803:5: note:   template argument deduction/substitution failed:
test.cpp:24:17: note:   mismatched types 'std::basic_istream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf >> (*tmp);
                 ^
In file included from test.cpp:2:0:
%LibaryPath%/istream:761:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)
     operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
     ^
%LibaryPath%/istream:761:5: note:   template argument deduction/substitution failed:
test.cpp:24:17: note:   mismatched types 'std::basic_istream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf >> (*tmp);
                 ^
In file included from test.cpp:2:0:
%LibaryPath%/istream:756:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
     operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
     ^
%LibaryPath%/istream:756:5: note:   template argument deduction/substitution failed:
test.cpp:24:17: note:   mismatched types 'std::basic_istream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf >> (*tmp);
                 ^
In file included from %LibaryPath%/istream:934:0,
                 from test.cpp:2:
%LibaryPath%/bits/istream.tcc:923:5: note: candidate: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)
     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
     ^
%LibaryPath%/bits/istream.tcc:923:5: note:   template argument deduction/substitution failed:
test.cpp:24:17: note:   mismatched types 'std::basic_istream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf >> (*tmp);
                 ^
In file included from %LibaryPath%/istream:934:0,
                 from test.cpp:2:
%LibaryPath%/bits/istream.tcc:955:5: note: candidate: template<class _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*)
     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
     ^
%LibaryPath%/bits/istream.tcc:955:5: note:   template argument deduction/substitution failed:
test.cpp:24:17: note:   mismatched types 'std::basic_istream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf >> (*tmp);
                 ^
In file included from %LibaryPath%/string:53:0,
                 from %LibaryPath%/bits/locale_classes.h:40,
                 from %LibaryPath%/bits/ios_base.h:41,
                 from %LibaryPath%/ios:42,
                 from %LibaryPath%/istream:38,
                 from test.cpp:2:
%LibaryPath%/bits/basic_string.tcc:1441:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
     operator>>(basic_istream<_CharT, _Traits>& __in,
     ^
%LibaryPath%/bits/basic_string.tcc:1441:5: note:   template argument deduction/substitution failed:
test.cpp:24:17: note:   mismatched types 'std::basic_istream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
     buf >> (*tmp);
                 ^
test.cpp:8:26: note: candidate: std::istream& operator>>(std::istream&, Test&)
     friend std::istream& operator >> (std::istream& lhs, Test& rhs) { rhs.onLoad(lhs); return lhs; }
                          ^
test.cpp:8:26: note:   no known conversion for argument 1 from 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}' to 'std::istream& {aka std::basic_istream<char>&}'
std::stringstream& buf();
buf << (*this); //!< ERROR HERE

此处将buf声明为返回对弦流的引用的函数。如果这是您的意图,并且您已经在其他地方定义了该功能,则需要调用该功能以获取参考:

buf() << *this;

如果您打算将buf作为本地流,则声明将只是

 std::stringstream   buf;

没有&()

buf是一个函数声明,没有参数,返回类型为std :: stringstream&amp;。这就是为什么g 显示没有运算符匹配错误的原因。

使用默认构造函数创建本地对象时,您应该创建它,例如

std :: stringstream buf;

它不会删除操作员匹配错误。

相关内容

  • 没有找到相关文章

最新更新