Solved

VC++ namespace and << stream operator

Posted on 1998-03-10
4
362 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

630 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