无法将 ostream << 重载与 stringstream 一起使用,参数 1 没有已知的转换
Unable to use ostream << overload with stringstream no known conversion for argument 1
我在尝试使用 << 的重载写入字符串流时 for ostream gcc 失败:
错误:'operator<<' 不匹配(操作数类型为 'std::stringstream&() {aka std::__cxx11::basic_stringstream&()}' 和 '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 obj\Debug\test.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 是一个没有参数的函数声明,return 类型为 std::stringstream&。这就是为什么 g++ 没有显示运算符匹配错误的原因。
使用默认构造函数创建本地对象时,您应该像这样创建它,
std::stringstream 缓冲区;
它不会消除运算符匹配错误。
我在尝试使用 << 的重载写入字符串流时 for ostream gcc 失败:
错误:'operator<<' 不匹配(操作数类型为 'std::stringstream&() {aka std::__cxx11::basic_stringstream&()}' 和 '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 obj\Debug\test.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 是一个没有参数的函数声明,return 类型为 std::stringstream&。这就是为什么 g++ 没有显示运算符匹配错误的原因。
使用默认构造函数创建本地对象时,您应该像这样创建它,
std::stringstream 缓冲区;
它不会消除运算符匹配错误。