2

Based on the figure below, I wrote my code. enter image description here

This is the code I wrote:

#include<iostream>
#include<string>
using namespace std;

class person
{
private:
    int code;
    string name;
public:
    void    setCode(int c) { code=c; }
    int getCode()          { return code; }
    void setName(string s) { name=s; }
    string getName()       { return name; }
};

class account : public person
{
private:
    double pay;
public:
    void    setPay(double p) { pay=p; }
    double getPay()          { return pay; }
};

class admin : public person
{
private:
    string experience;
public:
    void setExper(string e) { experience=e; }
    string getExper()       { return experience; }
};

class master : public account, public admin
{
};

int main()
{
    master mastObj;// create master object.
    mastObj.setName("John");
    system("pause");//to pause console screen, remove it if u r in linux
    return 0;
}

The compiler showed these errors:

Error   1   error C2385: ambiguous access of 'setName'
Error   2   error C3861: 'setName': identifier not found    
Error   3   IntelliSense: "master::setName" is ambiguous
4
  • First of all, your question title is not an actual question, regardless of the question mark at the end of the sentence. Secondly, it is better to actually describe a problem, explain what you have tried so far, instead of just pasting a piece of code and have others fix it for you. Commented Dec 1, 2011 at 11:22
  • Your code formatting was a bit of a mess. Code is for humans too :) Especially since you want us to read it. Commented Dec 1, 2011 at 11:31
  • possible duplicate of Virtual Inheritance Confusion Commented Dec 1, 2011 at 11:35
  • @sehe: I think it's safe to say that code is predominantly for humans :-) Commented Dec 1, 2011 at 11:53

3 Answers 3

7

It is classic example of Diamond Problem in C++ when you use multiple inheritance.

The solution is : Virtual inheritance

That is, you should do this:

class account : public virtual person 
{                   //^^^^^^^note this
   //code
};

class admin : public virtual  person
{                  //^^^^^^^note this
   //code
};

I just found really good posts on this site, so I would redirect you to those answers here:

which also means, this topic should be voted for close.

Sign up to request clarification or add additional context in comments.

Comments

3

You need virtual inheritance:

class account: public virtual person{
....
}

class admin: public virtual  person{
...
}

PS And your pay, code fields lack initialization! This could lead to embarassing errors (like paying the cleaner several million of dollars :)):

#include<iostream>
#include<string>
using namespace std;

class person
{
   // ...
   person() : code(0), name("anonymous") {}

};

class account : public virtual person
{
    // ...
    account() : pay(0) {}
};

class admin : public virtual person
{
    // ...
    admin() : experience("unknown") {}
};

Comments

0

you may use mastObj.master::admin::person::setName("John");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.