Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

VC++ namespace and << stream operator

When using #include <string> and using namespace std
I get error C2872: 'ostream' : ambiguous symbol when trying to overload the << operator.  

I can fix that by using the scope operator and calling ostream like ::ostream.

The only problem is then when I try and use the >> operator to print out the string I get this

error C2679: binary '<<' : no operator defined which takes a
right-hand operand of type 'class
std::basic_string,class
std::allocator>' (or there is no acceptable conversion)

My question is how do I make this work?  and why do I get the ambigious symbol error?

0
sfranks
Asked:
sfranks
  • 2
  • 2
1 Solution
 
Tommy HuiCommented:
Can you post a sample that demonstrates your problem?

Alternatively, can you try this on your compiler?

#include <string>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
      string x = "Hello World!";
      cout << x << endl;
      return 0;
}

Does this do what you want?
0
 
sfranksAuthor Commented:
Here is some sample code that illustrates a problem similiar to mine.


#include <string>
#include <iostream>
#include <strstrea.h>
#include <fstream.h>

using namespace std ;

void main()
{
  string s1;    
  s1="Hi there";
  cout << endl << "You entered: " << s1;

}

MS visual C++ 5.0
This code generates
E:\project\319\stringTest\sTest.cpp(12) : error C2872: 'cout' : ambiguous symbol


E:\project\319\stringTest\sTest.cpp(11) : error C2784: 'class std::basic_ostream<`template-parameter-1',`template-parameter-2'> &__cdecl std::endl(class std::basic_ostream<`template-parameter-1',`template-parameter-2'> &)' : could not deduce template argument for 'class std::basic_ostream<`template-parameter-1',`template-parameter-2'> &' from 'class ostream &'
E:\project\319\stringTest\sTest.cpp(11) : error C2784: 'class std::basic_ostream<`template-parameter-1',`template-parameter-2'> &__cdecl std::endl(class std::basic_ostream<`template-parameter-1',`template-parameter-2'> &)' : could not deduce template argument for 'class std::basic_ostream<`template-parameter-1',`template-parameter-2'> &' from 'class ostream &'
E:\project\319\stringTest\sTest.cpp(11) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char>>' (or there is no acceptable conversion)


so what I want to know if why there is that ambigious symbol error and the other << error.
0
 
Tommy HuiCommented:
You cannot mix and match between the new streams and the old streams. You are mixing by including string and iostream (new) and strstream.h and fstream.h (old).

You need to switch to the new completely or to the old completely.

0
 
sfranksAuthor Commented:
I rate your answer as good.  If you had explained it more I would have said excellent but your answer right now isn't totally acurate.  

String isn't a stream, and it isn't that you can't just mix and match the old stream classes and the new ANSI standard C++ stream classes it is that you can't mix and match C++ standard classes and the old stream libraries.  So you can't use #include <string> and #include <iostream.h>.  If you are going to use #include <string> then you must use ANSI standard libraries so just omit the .h so #include <iostream>.  

I decided to actually try and leaf through the MS documentation and I found information and actually came to the same piece of information.  Thanks for your time though.  Here are you 200 points.....   I guess next time I'll spend a little more time digging for the answer.  
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now