2

I have the following classes:

The Automata Class

#ifndef Automata_H
#define Automata_H

class Automata {

protected:
   // ...
public:
   virtual DFA* dfaEquivalent() {}
   // ....
};

The DFA class that inherits from Automata

#include "Automata.hpp"
#ifndef DFA_H
#define DFA_H

class DFA : public Automata 
{
private:
public:
   DFA() {}
};

And finnaly that inherits from DFA:

#include "DFA.hpp"

#ifndef _NFA_H
#define _NFA_H

class NFA : public DFA
{
private:
public:
   NFA() { }
   DFA* dfaEquivalent()
   {}
};
#endif

The problem comes when I have an instance of NFA and I want to call dfaEquivalent, and the compiler says the following :

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

What's the mistake I am committing with inheritance?

JeJo
  • 30,635
  • 6
  • 49
  • 88
k1k4ss0
  • 139
  • 4

1 Answers1

2

You are missing a forward declaration in the base class (i.e. in Automata.h ) header.

The compiler does not know what is the type DFA at the time, it compiles the Automata.h header (i.e. in the virtual function of Automata class)

virtual DFA* dfaEquivalent(){}
//      ^^^^--> unknown type

Since it is a pointer to a type DFA, providing a forward declaration for DFA in the Automata.h the header will solve the issue.

#ifndef Automata_H
#define Automata_H

class DFA; // forward declaration

class Automata 
{
public:
   virtual DFA* dfaEquivalent() {}
   // ...code
};
#endif

As a side note, have a look: When to use virtual destructors?. Your Automata might need one if you store the child class object to pointer to Automata.

JeJo
  • 30,635
  • 6
  • 49
  • 88