Fixing C++ Multiple Inheritance Ambiguous Call -


i have 3 classes structured this:

#include <iostream> using namespace std;  class keyword {     public:         virtual float getvalue() = 0; };  class characterkeyword : public keyword {     public:         virtual float getvalue(){return _value;}     private:         float _value; };  class measurementkeyword : public keyword {     public:         virtual float getvalue(){return _value;}     private:         float _value; };  class addresstype : public characterkeyword, public measurementkeyword {      private:         float address;         float addresext; };  int main() {     addresstype *a = new addresstype();     a->getvalue();     return 0; } 

i getting following:

in function ‘int main()’:
error: request member ‘getvalue’ ambiguous
error: candidates are: virtual float keyword::getvalue()
error: virtual float measurementkeyword::getvalue()
error: virtual float characterkeyword::getvalue()

i have done reading multiple inheritance , know has lot of pitfalls - being 1 of them. need class structure wondering if there way fix using templates?

update
after reading comments, original thought maybe can delineate between addresstype characterkeyword , addresstype measurementkeyword templating addresstype. , using such in updated code. or can specify namespace of member like. since templated way has not been mentioned yet answer, bad fix? should specify namespace of member want?

template <class t> class addresstype : public t {      private:         float address;         float addresext; };  int main() {     addresstype<measurementkeyword> *a = new addresstype<measurementkeyword>();     a->getvalue();     return 0; } 

this because of diamond inheritance pattern, resolve error can specify specific namespace want member like.

paddresstype->measurementkeyword::getvalue() 

or

paddresstype->characterkeyword::getvalue()   

  1. basically addresstype class has access getvalue members both classes inherits , can't choose 1 (call ambiguous).
  2. the scope resolution operator (:: ) helps specify 1 want.
  3. you haven't said want code i'll complex inheritance patterns not conducive creating readable code, rethink want.

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -