Pregunta

Error: Line 12 of Cell.h: 'Actor' undeclared identifier.

If I try to forward declare above it, it says that there's a redefinition. What do I do?

Actor.h:

#ifndef ACTOR_H
#define ACTOR_H
#include <iostream>
#include <vector>
#include <string>
#include "Cell.h"
using namespace std;

class Actor //Simple class as a test dummy. 
{
    public: 
        Actor();
        ~Actor();

};
#endif

Cell.h:

#include <iostream>
#include <string>
#include <vector>
#include "Actor.h"
#ifndef CELL_H
#define CELL_H
using namespace std;

class Cell   // Object to hold Actors. 
{
    private:
    vector <Actor*> test;
public:
    Cell();
    ~Cell();
    vector <Actor*> getTest();
    void setTest(Actor*);
};

#endif

Cell.cpp:

#include "Cell.h"
#include <vector>

vector<Actor*> Cell::getTest()  //These functions also at one point stated that
{                               // they were incompatible with the prototype, even 
}                               // when they matched perfectly.
void Cell::setTest(Actor*)
{
}

What else can I do?

¿Fue útil?

Solución

You have recursive #includes via your mutual references between cell.h and actor.h.

In Cell.h, delete #include <Actor.h>.

In Cell.h, add the line class Actor; just above the definition of class Cell.

In Cell.cpp, you might need to add #include "Actor.h".

Otros consejos

Remove the #include "Cell.h" from Actor.h and you're set to go.

In general, prefer forward declarations where you can, and includes where you must. I'd also replace the #include "Actor.h" from Cell.h with a forward declaration: class Actor;.

In the cpp files you can include the headers if you need them.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top