Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3221494
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:52:57+00:00 2026-05-17T15:52:57+00:00

The following code is giving me a compilation error. Can anyone please tell me

  • 0

The following code is giving me a compilation error. Can anyone please tell me why?

class mytype {
public:
    int value;
    mytype(int a) {
        value = a;
    }
    friend ostream& operator<<(ostream& stream, const mytype& a) {
        stream << a.value;//works
        return stream;
    }
    friend ostringstream& operator<<(ostringstream& stream, const mytype& a) {
        stream << (a.value);//compilation error
        return stream;
    }
};

Error:

error C2027: use of undefined type
‘std::basic_ostringstream<_Elem,_Traits,_Alloc>’

Upon fixing that:

error C2666: ‘operator <<‘ : 18 overloads have similar conversions

Final fix:

Declare constructor as explicit. Works on MSVC then.

I wonder why.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:52:58+00:00Added an answer on May 17, 2026 at 3:52 pm

    There is an overload ambiguity in the call to operator<< in the ostringstream overload.

    stream << a.value;
    

    There are a number of operator<< overloads that are members of the ostream class, which is a base class of ostringstream. One of these is declared as:

    ostream& ostream::operator<<(int);
    

    This overload is an exact match for the right-hand side (a.value is an int) but requires a derived-to-base conversion on the left-hand side (stream is an ostringstream, which is derived from ostream).

    However, there is also your ostringstream overload:

    ostringstream& operator<<(ostringstream&, const mytype&);
    

    This overload is an exact match for the left-hand side (stream is an ostringstream), and a user-defined converting constructor (your mytype(int) constructor) can be used to convert a.value (an int) to a mytype.

    Since one overload matches the first argument better and the other overload matches the second argument better, there is an ambiguity. You can fix this either by:

    • Explicitly converting the left-hand side to an ostream (using (ostream&)stream = a.value;), or
    • Remove the user-defined conversion by making the constructor explicit.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following code is giving compilation error in visual studio 2009. #include <iterator> #include <vector>
I have following code for which eclipse giving following compilation error: Multiple markers at
The following code is in the /Courses/Detail action: [AcceptVerbs(GET)] public ActionResult Detail(int id) {
The following code keeps giving me this error Parse error: syntax error, unexpected T_VARIABLE
Giving the following code: Animal a = new Animal { Name = Rover, Type
I have the following code in extjs and while rendering it is giving me
I have the following code loop(Data) -> receive {Key, Value} -> [{Key, Value}|Data]; {Key}
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
The following code works great in IE, but not in FF or Safari. I
The following code doesn't compile with gcc, but does with Visual Studio: template <typename

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.