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 8660397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:14:36+00:00 2026-06-12T16:14:36+00:00

This title has been used many times but after searching around 5-6 examples, I

  • 0

This title has been used many times but after searching around 5-6 examples, I couldn’t find anything that matches my problem:

I have a simple inheritance practice.

Class A is a base class, classes B & C inherit from it:

class A {

};

class B : public A { public:    int i; };

class C : public A { public:    int j; };

And a class P containing overloaded functions like this:

class P
{
public:
    void change(B *b)
    {
        b->i =1;
    }

    void change(C *c)
    {
        c->j =1;
    }
};

And when I use the functions like the following:

int main()
{
    A *b = new B();
    A *c = new C();
    P p;
    p.change(b);
    p.change(c);
    return 0;
}

it gives an error saying:

inherit2.cpp: In function ‘int main()’:
inherit2.cpp:37:12: error: call of overloaded ‘change(A*&)’ is ambiguous
inherit2.cpp:37:12: note: candidates are:
inherit2.cpp:21:7: note: void P::change(B*) <near match>
inherit2.cpp:21:7: note:   no known conversion for argument 1 from ‘A*’ to ‘B*’
inherit2.cpp:26:7: note: void P::change(C*) <near match>
inherit2.cpp:26:7: note:   no known conversion for argument 1 from ‘A*’ to ‘C*’
inherit2.cpp:38:12: error: call of overloaded ‘change(A*&)’ is ambiguous
inherit2.cpp:38:12: note: candidates are:
inherit2.cpp:21:7: note: void P::change(B*) <near match>
inherit2.cpp:21:7: note:   no known conversion for argument 1 from ‘A*’ to ‘B*’
inherit2.cpp:26:7: note: void P::change(C*) <near match>
inherit2.cpp:26:7: note:   no known conversion for argument 1 from ‘A*’ to ‘C*’

I will appreciate if you please help me solve the problem.
rahman

  • 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-06-12T16:14:37+00:00Added an answer on June 12, 2026 at 4:14 pm
    Polymorphism is one answer
    
    class A
    {
    public:
        virtual void set() = 0;
    };
    
    
    class B : public A
    {
    public:
        virtual void set() { i = 1; }
    private:
        int i;
    };
    
    class C : public A
    {
    public:
        virtual void set() { j = 1; }
    private:
        int j;
    };
    
    class P
    {
    public:
        void change(A *a)
        {
            a->set();
        }
    };
    
    int main()
    {
        A *b = new B();
        A *c = new C();
        P p;
        p.change(b);
        p.change(c);
        return 0;
    }
    

    With polymorphism there is no need for multiple change methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems like this title has been used many times before, unfortunately I don't
I know this has been an issue for others, but I've yet to find
This question has been asked before: AlertDialog custom title has black border But was
(Really strugging to title this question, so if anyone has suggestions feel free.) Say
I have a stored procedure that has this line: SET @SQL = 'SELECT path,title,tags
The response data in my test has this line: <head><title> My Title </title><meta charset
I'm sorry if this title doesn't describe the problem properly but I wasn't sure
Yes I know, this title isn't really helpfull but this is the exact problem.
i didn't really know how to title this question, but here's a thing that
I realize this question is very likely to have been asked before, but I've

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.