【发布时间】:2011-11-02 12:52:50
【问题描述】:
我主要有以下几点:
Sum *sum = new Sum(Identifier("aNum1"), Identifier("aNum2"));
我的课程是:
class Table {
private:
static map<string, int> m;
public:
static int lookup(string ident)
{
return m.find(ident)->second;
}
static void insert(string ident, int aValue)
{
m.insert(pair<string, int>(ident, aValue));
}
};
class Expression {
public:
virtual int const getValue() = 0;
};
class Identifier : Expression {
private:
string ident;
public:
Identifier(string _ident) { ident = _ident; }
int const getValue() { return Table::lookup(ident); }
};
class BinaryExpression : public Expression {
protected:
Expression *firstExp;
Expression *secondExp;
public:
BinaryExpression(Expression &_firstExp, Expression &_secondExp) {
firstExp = &_firstExp;
secondExp = &_secondExp;
}
};
class Sum : BinaryExpression {
public:
Sum(Expression &first, Expression &second) : BinaryExpression (first, second) {}
int const getValue()
{
return firstExp->getValue() + secondExp->getValue();
}
};
编译时出现以下错误:
没有匹配函数调用'Sum::Sum(Identifier, Identifier)'
候选者是:Sum::Sum(Expression&, Expression&)
Identifier 类继承自 Expression,为什么会出现此错误?
【问题讨论】:
标签: c++ inheritance constructor object-slicing