2016-11-24 25 views
1

我试图写使用< <一个过载的ostream GCC一个字符串流时失败:无法使用ostream的<<超载与字符串流没有已知的转换为参数1

错误:不对应的“运营商< <“(操作数类型是 '的std :: stringstream的&(){又名性病:: __ cxx11 :: basic_stringstream &()}' 和 '常量测试')

至于我可以告诉操作者定义,我已经使用了与stringstream类似的代码(其他类的输入运算符用于重载istream),而使用同一个co mpiler。

例如代码:

// 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>&}' 

回答

1
std::stringstream& buf(); 
buf << (*this); //!< ERROR HERE 

这里buf被声明为返回到字符串流的引用的功能。如果这是你的意图,以及你在其他地方定义的函数,你需要调用函数来获取参考:

buf() << *this; 

如果你打算拥有buf作为本地流,声明将只是

std::stringstream buf; 

没有&()

0

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

创建时使用默认的构造函数的局部对象,你应该创建像,

的std :: stringstream的BUF;

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

相关问题