views:

169

answers:

7

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.operatorit 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 ]

+2  A: 

You need to call the operator like a function.

std::stringstream s;

operator<<(s, "Your string")
lrm
How do I concatenate more then one to the same stringstream???
Tony
Call it multiple times.
lrm
lrm
A: 

Cast to / construct string explicitly:

qss << "some text " << string(DDateTime::date2Oracle(dFrom)) 
    <<  " more text " <<  string(DDateTime::date2Oracle(dUntil));
agsamek
No, this is the wrong way. There must exist an overload of `operator<<` that takes a type compatible with the return type of the `date2Oracle` function as second argument.
Philipp
No - there need to be a conversion to string, which most probably exists. If you have downvoted my answer - please consider upvoting instead.
agsamek
I haven't downvoted. But in C++ you do conversions to string *by implementing `operator<<`* and then applying a stringstream or `boost::lexical_cast`, not the other way round! Also, `string(...)` is an old-style cast which should be avoided in C++.
Philipp
+1 for the comment. Syntax is a matter of taste.
agsamek
A: 

What's stopping you from doing:

stringstream s;
s << "some text" << (DDateTime::date2Oracle(dFrom)) << "more text" << (DDateTime::date2Oracle(dUntil)) ;
wheaties
Because of a compiler error, I said that in my question...
Tony
Why is this downvoted? It's the correct syntax. If this doesn't work, the call to the `date2Oracle` function is ambiguous and the question is in fact unrelated to stringstreams or `operator<<`.
Philipp
@Tony the important part is that I've enclosed the `DDateTime::date2Oracle` in parenthesis. Now it can't be confused by the `::` operator.
wheaties
But `::` and `()` have a much higher predecence than `<<`. This can't be the problem.
Philipp
+2  A: 

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.

stefaanv
will that produce the same effect as doing qss << "text" << Somefunc() << "more text"; ???
Tony
operator<< should return the stream so it can be used by the next operator<<. This is called chaining and should be the same as the function-notation
stefaanv
+3  A: 

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
}
Philipp
+2  A: 

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.

vava
explanation+1, also try what agsamek suggested.
rubenvb
date2Oracle() returns a std::string
Tony
@Tony, then check error message again. `stringstream` does work with `std::string` out of the box and the issue is somewhere else.
vava
+1  A: 

Shouldn't be enough with qss << "some text " << DDateTime...?

rturrado