Reputation: 63300
I cannot get the following stringstreamm to compile
stringstream qss;
qss.operator << "some text " ::stringstream.operator << DDateTime::date2Oracle(dFrom) ::stringstream.operator << " more text " ::stringstream.operator << DDateTime::date2Oracle(dUntil);
If I just use the <<
operator without the ::stringstream.operator
it complains about the operator being ambigious, now it complains about incorrect syntax...
error C2143: syntax error : missing ';' before 'std::stringstream'
EDIT:
error C2593: 'operator <<' is ambiguous c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\ostream(434): could be 'std::basic_ostream<_Elem,_Traits>::_Myt &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits>::_Mysb *)' with [ _Elem=char, _Traits=std::char_traits ]
Upvotes: 0
Views: 1743
Reputation: 49850
The operator
keywords don't belong here, leave them out:
qss << "some text" << DDateTime::date2Oracle(dFrom) << " more text " << DDateTime::date2Oracle(dUntil);
This should be perfectly valid and unambiguous, unless the date2Oracle
function is ambiguously overloaded.
The correct pattern for implementing operator<<
for a type T
is:
template<typename Char, typename Traits>
std::basic_ostream<Char, Traits>
operator<<(std::basic_ostream<Char, Traits>& stream, const T& object) {
// now put something into the stream
return stream; // return stream << xyz ... is also possible
}
Upvotes: 3
Reputation: 2679
You need to call the operator like a function.
std::stringstream s;
operator<<(s, "Your string")
Upvotes: 2
Reputation: 25401
Well, it is obvious that whatever type DDateTime::date2Oracle(dFrom)
returns does not implement <<
operator. So you will have to write one yourself.
As for the syntax, first of all you have to call it just like a function which it actually is:
stringstream qss;
operator<<(
(operator<<(qss << "some text ",
DDateTime::date2Oracle(dFrom)) << " more text "),
DDateTime::date2Oracle(dUntil));
And second of all, stringstream
defined in std
namespace, so you have to write it like std::stringstream
or ::std::stringstream
. ::stringstream
will look for it in global namespace and there is no such class defined there.
BTW, operator<<
usually is implemented as free function, so qss.operator<<
wouldn't work.
Upvotes: 2
Reputation: 14392
Go really funky:
qss.operator <<("some text ");
qss.operator <<(DDateTime::date2Oracle(dFrom));
qss.operator <<(" more text ");
qss.operator <<(DDateTime::date2Oracle(dUntil));
And you'll probably get a better idea where the ambiguity is.
Upvotes: 2
Reputation: 35980
What's stopping you from doing:
stringstream s;
s << "some text" << (DDateTime::date2Oracle(dFrom)) << "more text" << (DDateTime::date2Oracle(dUntil)) ;
Upvotes: 0
Reputation: 9074
Cast to / construct string explicitly:
qss << "some text " << string(DDateTime::date2Oracle(dFrom))
<< " more text " << string(DDateTime::date2Oracle(dUntil));
Upvotes: 1