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
sehe
  • 374,641
  • 47
  • 450
  • 633
Aan
  • 12,247
  • 36
  • 89
  • 150
  • 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. – Aron Rotteveel Dec 01 '11 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. – sehe Dec 01 '11 at 11:31
  • possible duplicate of [Virtual Inheritance Confusion](http://stackoverflow.com/questions/8148470/virtual-inheritance-confusion) – Nawaz Dec 01 '11 at 11:35
  • @sehe: I think it's safe to say that code is *predominantly* for humans :-) – Kerrek SB Dec 01 '11 at 11:53

3 Answers3

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.

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
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") {}
};
sehe
  • 374,641
  • 47
  • 450
  • 633
0

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

db-hopper
  • 23
  • 6