关于c ++:无法使用ostream<<

Unable to use ostream << overload with stringstream no known conversion for argument 1

我在尝试使用重载<

错误:STD:"操作数类型不匹配"(STD::StrugStand &);({ AKA STD:::Y.OXCXX11::Basic StrugStand &()}和‘const test’)

据我所知,这个操作符是被定义的,我使用了与Stringstream类似的代码(我为Istream重载的其他类的输入操作符),没有使用相同编译器的任何问题。

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 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;
}

筛选的生成输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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>&}'


BUF是一个函数声明,没有参数和返回类型,如STD::StrugStand和。这就是为什么G++没有显示运算符匹配错误的原因。

当使用默认构造函数创建本地对象时,应该像,

STD:StrugSufBuf;

它不会删除任何运算符匹配错误。


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

这里,buf声明为返回对stringstream的引用的函数。如果这是您的意图,并且您在其他地方定义了该函数,则需要调用该函数以获取引用:

1
buf() << *this;

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

1
 std::stringstream   buf;

没有&()