实现抽象类

时间:2014-05-12 00:55:24

标签: c++

我是C ++菜鸟,我有一个noob问题。

我在头文件中有一个抽象的树类定义。我知道它是抽象的,因为它包含虚拟方法。它看起来像这样:

//AbsTree.h

class AbsTree { 
   public:
       AbsTree();
       virtual ~AbsTree() =0;
       virtual void accept(Visitor &visitor) const = 0;
       virtual void execute(Env &ctxt) const throw (InappropriateFunction);
};

class Operation: public AbsTree{
   public:
      Operation(char oper);
      virtual ~Operation();
      virtual void accept (Visitor &visitor) const;
      char getOperator() const;
   private:
      char operator;
};

我已经提出了以下AbsTree的实现:

AbsTree.cpp

#include "AbsTree.h"

AbsTree::AbsTree() {}
AbsTree::~AbsTree() {}
AbsTree::accept(Visitor &visitor) const{}
AbsTree::execute(Env &ctxt) const {}

Operation::Operation(char oper): operator(oper) {}
Operation::~Operation(){}
Operation::accept(Visitor &visitor) const{}
char Operation::getOperator() {
   return operator;
}

所以我尝试通过:

编译.cpp
g++ AbsTree.cpp -o AbsTree

我得到了

AbsTree.cpp:3:1: error: 'AbsTree' does not name a type
AbsTree.cpp:4:1: error: 'AbsTree' does not name a type
AbsTree.cpp:5:1: error: 'AbsTree' does not name a type
AbsTree.cpp:6:1: error: 'AbsTree' does not name a type
AbsTree.cpp:8:1: error: 'Operation' does not name a type
AbsTree.cpp:9:1: error: 'Operation' does not name a type
AbsTree.cpp:10:1: error: 'Operation' does not name a type
AbsTree.cpp:11:6: error: 'Operation' has not been declared
AbsTree.cpp: In function 'char getOperator()':
AbsTree.cpp:12:9: error: 'operator' was not declared in this scope

请告诉我我的C ++菜鸟。

2 个答案:

答案 0 :(得分:3)

以下是代码的可编译/可运行版本(可在ideone here运行)

struct Visitor { };
struct Env { };
struct InappropriateFunction { };

class AbsTree { 
   public:
       AbsTree();
       virtual ~AbsTree() =0;
       virtual void accept(Visitor &visitor) const = 0;
       virtual void execute(Env &ctxt) const throw (InappropriateFunction);
};

class Operation: public AbsTree{
   public:
      Operation(char oper);
      virtual ~Operation();
      virtual void accept (Visitor &visitor) const;
      char getOperator() const;
   private:
      char operator_;
};

AbsTree::AbsTree() {}
AbsTree::~AbsTree() {}
void AbsTree::accept(Visitor &visitor) const{}
void AbsTree::execute(Env &ctxt) const throw (InappropriateFunction) {}

Operation::Operation(char oper): operator_(oper) {}
Operation::~Operation(){}
void Operation::accept(Visitor &visitor) const{}
char Operation::getOperator() const {
   return operator_;
}

int main()
{
    Operation o('x');
    Visitor my_visitor;
    o.accept(my_visitor);
    o.getOperator();
}

基本上,您必须更加谨慎,以确保您定义的函数符合您的声明 - 包括constthrow规范和返回类型。此外,operator是保留关键字,您不能使用它来命名自己的变量。

另外,不推荐使用异常规范...最好将它们排除在外,因为它们从未被证明有用。 (C ++ 11有一个noexcept关键字,如果你想清楚一个函数可以在代码中使用异常保证,它偶尔会有用 - 可能会得到一些额外的优化,但如果引发异常/在您的程序将调用std::terminate)的函数下。

答案 1 :(得分:1)

AbsTree是一个抽象类,就像你说的那样。这意味着您需要创建派生的类才能提供实现。

// ConcreteTree.h

#include "AbsTree.h"

class ConcreteTree : public AbsTree {
   public:
       ConcreteTree();
       virtual ~ConcreteTree();
       virtual void accept(Visitor &visitor) const;
       virtual void execute(Env &ctxt) const throw (InappropriateFunction);
}

然后:

// ConcreteTree.cpp

#include "ConcreteTree.h"

ConcreteTree::ConcreteTree() {}
ConcreteTree::~ConcreteTree() {}
ConcreteTree::accept(Visitor &visitor) const{}
ConcreteTree::execute(Env &ctxt) const {}