Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VC++ namespace and << stream operator

Posted on 1998-03-10
4
Medium Priority
?
364 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 600 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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

715 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