0

** Reworded to make more sense

i have 3 classes and i want instances of these classes to be able to interact with each other, not by having a controller object. The problem i'm having is they're not defined in each others .h files and i don't know how to do that properly. Below is some code to explain;

game.cpp:

#include "game.h"
#include "Class - cEntity.h"
#include "Class - cGUI.h"

cGui *gui;

vector<cEntity*>    entities;

Class - cEntity.h:

#include "game.h"
#include "Class - cGui.h"

extern cGui *gui;

class cEntity{
...
};

I compile the code that uses this structure, and i get 2 errors;

Error 7 error C2143: syntax error : missing ';' before '*' c:\dropbox\of_v0.8.0_vs_release\apps\myapps\zombierts\src\entities.h 10

Error 8 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\dropbox\of_v0.8.0_vs_release\apps\myapps\zombierts\src\entities.h 10

Can anyone help clarify where i'm going wrong?

Thanks

Gravious
  • 35
  • 5

3 Answers3

1

You can make a header for the vector. Something like this:

cEntity.h:

class cEntity
{
    // ...
};

interests.h:

#include <vector>

class cEntity;

extern std::vector<cEntity*> interests;

Now for the implementations:

cEntity.cpp:

#include "cEntity.h"

// implement member functions and define static data members

interests.cpp:

#include "interests.h"

std::vector<cEntity*> interests;

In all places where you need to refer to the vector, add #include "interests.h", and if you need to operate on actual entities, also #include "cEntity.h".

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

Use singleton pattern. code from C++ Singleton design pattern. And then you can have vector interests; in your singleton class.

class S
{
    public:
        static S& getInstance()
        {
            static S    instance; // Guaranteed to be destroyed.
                                  // Instantiated on first use.
            return instance;
        }
    private:
        S() {};                   // Constructor? (the {} brackets) are needed here.
        // Dont forget to declare these two. You want to make sure they
        // are unaccessable otherwise you may accidently get copies of
        // your singleton appearing.
        S(S const&);              // Don't Implement
        void operator=(S const&); // Don't implement
};
Community
  • 1
  • 1
Jake Lin
  • 11,146
  • 6
  • 29
  • 40
  • Just a note, in C++11, a class can be made non-copyable using the `delete` keyword, http://stackoverflow.com/questions/9458741/with-explicitly-deleted-member-functions-in-c11-is-it-still-worthwhile-to-inh – Prashant Kumar Aug 20 '13 at 23:44
0
  • If you are trying to access a container in another class, there are various ways you could do this. First, if you already have the two classes. You can declare a vector of cEntity to be public. You can even add Typedef std::vector cEntityType; cEntityType interests;
  • If your cEntity vector is public, you can create a class instance that contains this vector, lets say C class. Once you have the C class instance you can access cEntity container by calling it; c->interests or c.interests. This works because you are creating an instance of C class then accessing its public classes and public variables. You will be making copies of cEntity vector.
  • On the other hand if you are trying to make a vector of cEntity global and have one copy, then you can make the vector to be static. Thus you can access it anywhere and anyone can modify it.
Juniar
  • 1,269
  • 1
  • 15
  • 24