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

  • SEARCH
  • Home
  • 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 3799416
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:43:44+00:00 2026-05-19T13:43:44+00:00

I have a class using operator overloading, but there are some warnings. // base.h

  • 0

I have a class using operator overloading, but there are some warnings.

// base.h

class base {
public:
    base();
    base(int n);
    virtual ~base();
    virtual void printBase(std::ofstream & out);
    virtual base & operator =(const base &);
    friend std::ofstream & operator <<(std::ofstream & out, const base &);
private:
       double * coeff;
       int n;
};

// base.cpp

std::ofstream & operator<<(std::ofstream & fout, const base & obj)
{
    for(int i =0; i<(obj.n)-1; i++)
    {
        fout << obj.coeff[i]<<"*x"<<i;
        if(i<obj.n-2)
        {
            fout<<"+";
        }
    }
    fout<<"="<<obj.coeff[(obj.n)-1];
    return fout;
}

void base::printBase(std::ofstream & fout)
{
    for(int i =0; i<n-1; i++)
    {
        fout<<coeff[i]; // warning occurs here!!
        if(i<n-2)
        {
            fout<<"+";
        }
    }
    fout<<"="<<coeff[n-1];
}

The warning is:

>

 warning: ISO C++ says that these are ambiguous, even though the worst conversion for
 the first is better than the worst conversion for the second:
    c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/ostream.tcc:105:5: note:
 candidate 1: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
    ..\Hyperplane.cpp:55:17: note: candidate 2: std::ofstream& operator<<(std::ofstream&, const Hyperplane&)

From the above warning, it should be the problem of <<. I know the reason, but how could I handle this warning?

Thank you!

  • 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-19T13:43:45+00:00Added an answer on May 19, 2026 at 1:43 pm

    The problem is actually with one of your class’s constructors:

    base(int n);
    

    This constructor is what is called a converting constructor. It can be used to convert an int to a base, so this would be legal:

    base x = 42;
    

    If you don’t want to allow this implicit conversion, you can make the constructor explicit:

    explicit base(int n);
    

    The interesting question is “where is the ambiguity in fout << coeff[i];?

    There are two candidate functions that the compiler can’t decide between (or shouldn’t be able to decide between; your compiler is being “nice” to you): one is the built-in std::ostream operator<< overload that looks like this:

    std::ostream& operator<<(std::ostream&, double);
    

    the second is your operator overload that looks like:

    std::ofstream& operator<<(std::ofstream&, const base&);
    

    With the first candidate, the first argument requires a derived-to-base conversion: the std::ofstream needs to be converted to a std::ostream for the function to be called. The second argument, a double, matches exactly.

    With the second candidate, the first argument, a std::ofstream, matches exactly. The second argument requires the use of the built-in double to int conversion then the use of your converting constructor to convert from int to base.

    In order for the compiler to select one candidate function as the right one to call, each of the arguments must match the corresponding parameter of the candidate at least as well as it matches any of the other candidates.

    With these two candidates here, the first argument matches the second candidate better but the second argument matches the first candidate better, thus there is an ambiguity.

    An alternative solution would be to change your overload such that the first argument matches as well as the first argument of the built-in candidate:

    std::ostream& operator<<(std::ostream&, const base&);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: class A { public: A() {}; void operator[](int x)
I have some problems overloading operators with template members and using make_pair: class MyArchive
I have an application that opens another class using intent : private void createRepository(){
I have a problem with overloading of the < operator. I have this class:
I have a class using ReaderWriterLockSlim with a read method and a write method
How to select next n hidden elements that have class foo using jQuery?
I have written a class using std::tr1::regex, and I don't know how to link
I have defined the following class using COM to use the IGroupPolicyObject using C#.NET:
I have a class I'm using to handle all my DB interaction. Basically when
I am developing a web form using Visual Web Developer Currently I have class

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.