【发布时间】:2020-12-12 20:58:39
【问题描述】:
我有以下课程:
Automata 类
#ifndef Automata_H
#define Automata_H
class Automata {
protected:
// ...
public:
virtual DFA* dfaEquivalent() {}
// ....
};
继承自Automata的DFA类
#include "Automata.hpp"
#ifndef DFA_H
#define DFA_H
class DFA : public Automata
{
private:
public:
DFA() {}
};
最后继承自DFA:
#include "DFA.hpp"
#ifndef _NFA_H
#define _NFA_H
class NFA : public DFA
{
private:
public:
NFA() { }
DFA* dfaEquivalent()
{}
};
#endif
当我有一个NFA 的实例并且我想调用dfaEquivalent 并且编译器显示以下内容时,问题就出现了:
g++ -c -o main.o main.cpp
In file included from DFA.hpp:1:0,
from NFA.hpp:1,
from Comparador.hpp:5,
from main.cpp:2:
Automata.hpp:96:13: error: ‘DFA’ does not name a type; did you mean ‘DFA_H’?
virtual DFA* dfaEquivalent(){}
^~~
DFA_H
<builtin>: recipe for target 'main.o' failed
make: *** [main.o] Error 1
我在继承方面犯了什么错误?
【问题讨论】:
-
Automata对DFA一无所知。为什么要返回DFA*而不是Automata*? -
旁注:不要在标题保护之外包含文件。将它们包含在里面。
标签: c++ class oop inheritance virtual-functions