0

i have issues with inheritance.

I have 7 files: main.cpp,Utils.h,Utils.cpp,Loader.h,Loader.cpp,Calculation.h and Calculation.cpp.

Utils.h is inheriting from Loader.h and Calculation.h and

Loader.h is inheriting from Calculation.h.

1: main.cpp

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "Utils.h"

using namespace std;

int main()
{

Utils Tool;

Tool.Test();

return 0;
}

2: Utils.h

#ifndef UTILS_H_INCLUDED
#define UTILS_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>

#include "Calculation.h"
#include "Loader.h"
 

class  Utils:public Loader,public  Calculation
{
public:

Utils();

void Test();


~Utils();

};


#endif // UTILS_H_INCLUDED

3: Utils.cpp

#include "Utils.h"



Utils::Utils():Calculation(),Loader()
{


}

void Utils::Test()
{

printf("Hello World\n");

int a=10,b=10;

printf("Result is: %d\n",Add(a,b));

Func();
}

Utils::~Utils()
{

}

4: Loader.h

#ifndef LOADER_H_INCLUDED
#define LOADER_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

#include "Calculation.h"

class Loader:public Calculation
{

public:

Loader();

void Func();

~Loader();

};

5: Loader.cpp

#include "Loader.h"


Loader::Loader():Calculation()
{

}

void Loader::Func()
{

printf("Great Loader\n");
}

Loader::~Loader()
{

}

6: Calcuation.h

#ifndef CALCULATION_H_INCLUDED
#define CALCULATION_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

class Calculation
{
public:

Calculation();

int Add(int a,int b);

~Calculation();

};

#endif //CALCULATION_H_INCLUDED

7: Calculation.cpp

#include  "Calculation.h"


Calculation::Calculation()
{

}

int Calculation::Add(int a,int b)
{
return a+b;
}

Calculation::~Calculation()
{

}

When i try to compile the code it fails. I receive a error message:

C:\Users\MY_NAME\Documents\CodeBlocksProj\Inheritance_Test\Utils.cpp: In member function 'void Utils::Test()':
C:\Users\MY_NAME\Documents\CodeBlocksProj\Inheritance_Test\Utils.cpp:18:31: error: reference to 'Add' is ambiguous
  • `class Utils:public Loader,public Calculation` should probably just be `class Utils:public Loader` otherwise you inherit from `Calculation` twice – Alan Birtles Nov 28 '20 at 21:40
  • `Utils` derives from `Calculation` twice, so there's ambiguity about which base's function you want to call. – Kerrek SB Nov 28 '20 at 21:40
  • Thank you for your explanation but i have difficulties understanding your explanation,can you be more clear? – user3552351 Nov 29 '20 at 14:10

0 Answers0