【发布时间】:2014-04-26 18:58:56
【问题描述】:
为什么这不起作用?虚函数GetNOperands()和PerformOp()在BinaryOp类中定义,DoOp()在OpAdd类中定义。操作符类中的虚拟Prec() 函数也在 OpAdd 中定义。我研究了我认为不适用于此代码的“钻石问题”,因为派生类中的每个函数只有一个定义?代码如下:
#include <iostream>
#include <vector>
#define PREC_LOW 0
#include <assert.h>
//operator class - abstract
template <class T> class Op {
public:
virtual unsigned int GetNOperands() = 0;
virtual bool PerformOp( std::vector<T>& operands, T& result ) = 0;
virtual ~Op() {}
};
//binary operator class - for 2 operators - abstract
template <class T> class BinaryOp : public Op<T> {
public:
unsigned int GetNOperands();
bool PerformOp( std::vector<T>& operands, T& result );
virtual ~BinaryOp() {}
protected:
virtual bool DoOp( T first, T second, T& result ) = 0;
};
template <class T> class Operator : public Op<T> {
public:
virtual unsigned int Prec() = 0; //precedence
};
template <class T> class OpAdd : public BinaryOp<T>, public Operator<T> {
public:
unsigned int Prec();
private:
bool DoOp( T first, T second, T& result );
};
template <class T> unsigned int BinaryOp<T>::GetNOperands() {
return 2;
}
template <class T> bool BinaryOp<T>::PerformOp( std::vector<T>& operands, T& result ) {
assert( operands.size() == 2 );
return DoOp( operands.at(0),operands.at(1),result);
}
template <class T> bool OpAdd<T>::DoOp( T first, T second, T& result ) {
result = first + second;
return true;
}
template <class T> unsigned int OpAdd<T>::Prec() {
return PREC_LOW;
}
int main() {
OpAdd<int> a;
return 0;
}
编辑: 编译器错误状态:
source.cpp: In function 'int main()':
source.cpp:55:13: error: cannot declare variable 'a' to be of abstract type 'OpAdd<int>'
OpAdd<int> a;
^
source.cpp:30:29: note: because the following virtual functions are pure withi
n 'OpAdd<int>':
template <typename T> class OpAdd : public BinaryOp<T>, public Operator<T> {
^
source.cpp:10:23: note: unsigned int Op<T>::GetNOperands() [with T = int]
virtual unsigned int GetNOperands() = 0;
^
source.cpp:11:15: note: bool Op<T>::PerformOp(std::vector<T>&, T&) [with T = int]
virtual bool PerformOp( std::vector<T>& operands, T& result ) = 0;
【问题讨论】:
-
定义“不起作用”。
-
好吧,您没有覆盖
OpAdd中的direct 基类Operator<T>(及其基类)的虚函数。 -
我添加了编译错误
-
请阅读:不能声明抽象类型的变量/由于这些纯函数是抽象的...编译器正在拼出究竟是什么错误。
-
但是这些函数是在 BinaryOp 中定义的,是被 OpAdd 继承的?
标签: c++ class inheritance multiple-inheritance pure-virtual