Multipal inharitance error: conflicting

S

sumit15nov

Hi,
I am getting error on doing multipal inheritance.
Please check the below code-

class Hal
{
public:
virtual int SetPrintLoss()
{
cout << "Hal::SetPrintLoss" << endl;
}
};
class FluxGrid : public Hal
{
public:
void SomeFunction()
{
cout << "FluxGrid::SomeFunction" << endl;
}
};
class HalAmplifier
{
public:
virtual void SetPrintLoss()
{
cout << "HalAmplifier::SetPrintLoss" << endl;
}
};
class Simulator : public FluxGrid, public HalAmplifier
{
public:
virtual void SetPrintLoss()
{
cout << "Simulator::SetPrintLoss" << endl;
}
};

I am getting error : -
error: conflicting return type specified for ‘virtual void Simulator::SetPrintLoss()’
error: overriding ‘virtual int Hal::SetPrintLoss()’


please suggest me what to do now.
 
Ö

Öö Tiib

Hi,

I am getting error on doing multipal inheritance.
Please check the below code-

class Hal
{
public:
virtual int SetPrintLoss()
Type 'void' here ^^^ instead of 'int'.
please suggest me what to do now.

Enable warnings and pay attention to those. Most compilers warn that
Hal::SetPrintLoss() does not return anything despite being declared
int.
 
S

sumit15nov

Type 'void' here ^^^ instead of 'int'.







Enable warnings and pay attention to those. Most compilers warn that

Hal::SetPrintLoss() does not return anything despite being declared

int.


Thanks.
Agreed, Now Hal::SetPrintLoss() is returning 0.

the change code is -

class Hal
5 {
6 public:
7 virtual int SetPrintLoss()
8 {
9 cout << "Hal::SetPrintLoss" << endl;
10 return 0;
11 }
12 };
13 class FluxGrid : public Hal
14 {
15 public:
16 void SomeFunction()
17 {
18 cout << "FluxGrid::SomeFunction" << endl;
19 }
20 };
21 class HalAmplifier
22 {
23 public:
24 virtual void SetPrintLoss()
25 {
26 cout << "HalAmplifier::SetPrintLoss" << endl;
27 }
28 };
29 class Simulator : public FluxGrid, public HalAmplifier
30 {
31 public:
32 virtual void SetPrintLoss()
33 {
34 cout << "Simulator::SetPrintLoss" << endl;
35 }
36 };
 
Ö

Öö Tiib

Thanks.
Agreed, Now Hal::SetPrintLoss() is returning 0.
the change code is -

class Hal
5 {
6 public:
7 virtual int SetPrintLoss()
8 {
9 cout << "Hal::SetPrintLoss" << endl;
10 return 0;
11 }
12 };
13 class FluxGrid : public Hal
14 {
15 public:
16 void SomeFunction()
17 {
18 cout << "FluxGrid::SomeFunction" << endl;
19 }
20 };
21 class HalAmplifier
22 {
23 public:
24 virtual void SetPrintLoss()
25 {
26 cout << "HalAmplifier::SetPrintLoss" << endl;
27 }
28 };
29 class Simulator : public FluxGrid, public HalAmplifier
30 {
31 public:
32 virtual void SetPrintLoss()
33 {
34 cout << "Simulator::SetPrintLoss" << endl;
35 }
36 };

-----------------------------------------
sumit15nov@in-lnxbld99:~/code$ g++ -Wall -Wextra -pedantic -c multipal-inharitance-error.cpp
multipal-inharitance-error.cpp:42: error: conflicting return type specified for ‘virtual void Simulator::SetPrintLoss()’
multipal-inharitance-error.cpp:7: error: overriding ‘virtual int Hal::SetPrintLoss()’
sumit15nov@in-lnxbld99:~/code$

Problem is that you have overrides with same signature but non-compatible return type. There a re lot of solutions.
1) You can have different functions (for example rename 'Hal::SetPrintLoss()' as 'Hal::SetLoss()' )
2) You can make them valid overrides of same inheritance tree. Should havesame return type then.
3) You can make the signatures different and so turn them into overloads (for example have 'int Hal::SetPrintLoss(int)' instead of 'int Hal::SetPrintLoss()').
4) You can stop using language features that you do not understand in yourcode.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top