Solved

VC++ namespace and << stream operator

Posted on 1998-03-10
4
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

734 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