Solved

VC++ namespace and << stream operator

Posted on 1998-03-10
4
356 Views
Last Modified: 2013-12-14
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
Comment
Question by:sfranks
  • 2
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 200 total points
ID: 1183322
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
 
LVL 1

Author Comment

by:sfranks
ID: 1183323
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
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1183324
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
 
LVL 1

Author Comment

by:sfranks
ID: 1183325
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now