0
votes

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 ]

7
We need the second line where alternative is described :)vava
We need to know the type that DDateTime returns.Puppy
string DDateTime::date2Oracle(DATE Date)Tony The Lion
Yeah, I bet it is something that have conversion to std::string and to something else as well.vava

7 Answers

3
votes

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
}
2
votes

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.

2
votes

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.

2
votes

You need to call the operator like a function.

std::stringstream s;

operator<<(s, "Your string")
1
votes

Cast to / construct string explicitly:

qss << "some text " << string(DDateTime::date2Oracle(dFrom)) 
    <<  " more text " <<  string(DDateTime::date2Oracle(dUntil));
1
votes

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

0
votes

What's stopping you from doing:

stringstream s;
s << "some text" << (DDateTime::date2Oracle(dFrom)) << "more text" << (DDateTime::date2Oracle(dUntil)) ;