2012-12-03 41 views
-1

我试图做多重继承在C++:继承和地图C++

class Element{ 
public: 
    Element(): common_variable(0) {} 
    int common_variable; 
}; 

class Number:public Element, public Setupable{ 
public: 
Number() {} 
    vector<Digit> digit_vector; 
}; 
class MultiLed:public Element, public Setupable{ 
public: 
MultiLed() {} 
    vector<Led> led_vector; 
}; 

对象要素是从来没有实例化,但我用它来避免MULTILED和数字代码重复。

有一个地图,其中包括一个数:map<string,Number> mNumbers,我想它在第一次使用创建:

mNumbers["blabla"].digit_vector.push_back(digit); 

但是,这是行不通的。对Element,Setupable和Number的构造函数的调用已正确完成。但程序停止在“push_back”呼吁说:

undefined symbol: _ZN14AcElementD2Ev 

有人可以帮我这个吗?

+1

“Setupable”的定义也可能有帮助... – janr

+0

您是否使用GCC进行编译,并且确切的符号是?它看起来差不多,但不完全像GCC所称的名为'AcElement'类的析构函数。你有一个类似这样的名字的类,你在那个类中声明了一个析构函数吗? –

+0

@Jav元素中是否有静态成员D2Ev? – billz

回答

0

由于您没有发布所有代码,我必须猜测代码中的情况。当您更新问题时,我会更新我的答案。 最有可能的是,您已经在Element中声明了一个静态成员,名称很可能是D2Ev,但您忘记提供它的定义。

当您定义基类时,不要忘记声明虚拟析构函数。

class Element{ 
public: 
    Element():common_variable(0) { 
    } 
    virtual ~Element(){ 
    } 
    int common_variable; 
}; 
0

digit_vector是私人的,你正试图在课外访问。

1

你有两个问题在这里

  1. 访问限制:在所有class成员都默认为私有
  2. 你contructors没有定义,只是声明

补丁代码:

class Element{ 
public: 
    Element(){ 
     common_variable = 0; 
    } 
    int common_variable; 
}; 

class Number:public Element, public Setupable{ 
public: 
Number() {} 
    vector<Digit> digit_vector; 
}; 
class MultiLed:public Element, public Setupable{ 
public: 
MultiLed() {} 
    vector<Led> led_vector; 
}; 
0

只是你忘记设置公共方法和变量

class Number:public Element, public Setupable 
{ 
public: 
    Number(): Element(), Setupable() {} 
    vector<Digit> digit_vector; 
}; 

顺便说一下,就吃一个成员变量公众并不总是好主意, 你应该提供一个方法返回digit_vector

//the method is provided in 2 soups, and there are reson for that 
//even if you can omitt the "const" version, is better you keep both 
const vector<Digit> & getDigits() const { return digit_vector; } 
vector<Digit> & getDigits() { return digit_vector; } 

,或者如果你的类必须是只是一个“数据容器“让一个结构

struct Number:public Element, public Setupable 
{ 
    //default construsctors of Element and Setupable are still called! 
    //if not providing a default constructor, the compiler will create one 
    //automatically calling all default constructor (including the vector's 
    //one. Also digit_vector is constructed. 

    vector<Digit> digit_vector; 
}; 

结构是相同的类,唯一不同的是,默认情况下成员全部公开, 和桑尼你不必指定“public:”。一种好的风格是只使用类(并且你必须记住什么是公有或私有的),并且只需要使用结构就可以获得一个几乎没有数据容器的逻辑。

所以写:

class Number:public Element, public Setupable 
{ 
    //default construsctors of Element and Setupable are still called! 
    //if not providing a default constructor, the compiler will create one 
    //automatically calling all default constructor (including the vector's 
    //one. Also digit_vector is constructed. 

public: 
    vector<Digit> digit_vector; 
}; 

有效了。但通常你不想隐式地调用构造函数。

+0

好吧,我纠正了这个小错误(事实上,我错过了代码,因为“public:”已经写好了)。 – Jav

+0

我试过上面的代码,它编译得很好(当然使用空的Led和Digit)。如果出现错误,则不在您发布的代码或您的IDE设置中..查看我编译的代码: http://pastebin.com/E5YkV7ME – GameDeveloper

0

我忘了执行析构函数〜元素(){} 现在,它的工作原理...

对不起大家对这个愚蠢的错误。

Thx无论如何!