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

4

3 回答 3

1

您可以为向量制作标题。像这样的东西:

cEntity.h:

class cEntity
{
    // ...
};

兴趣.h:

#include <vector>

class cEntity;

extern std::vector<cEntity*> interests;

现在对于实现:

cEntity.cpp:

#include "cEntity.h"

// implement member functions and define static data members

兴趣.cpp:

#include "interests.h"

std::vector<cEntity*> interests;

在所有需要引用向量的地方,添加#include "interests.h",如果需要对实际实体进行操作,还可以使用#include "cEntity.h".

于 2013-08-20T23:44:41.270 回答
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
};
于 2013-08-20T23:32:32.503 回答
0
  • 如果您尝试访问另一个类中的容器,有多种方法可以做到这一点。首先,如果您已经拥有这两个课程。您可以将 cEntity 的向量声明为公共的。你甚至可以添加 Typedef std::vector cEntityType; cEntityType 兴趣;
  • 如果你的 cEntity 向量是公共的,你可以创建一个包含这个向量的类实例,比如说 C 类。一旦你有了 C 类实例,你就可以通过调用它来访问 cEntity 容器;c->兴趣或c.interests。这是有效的,因为您正在创建 C 类的实例,然后访问它的公共类和公共变量。您将制作 cEntity 向量的副本。
  • 另一方面,如果您尝试将 cEntity 的向量设为全局并拥有一个副本,则可以将向量设为静态。因此,您可以在任何地方访问它,并且任何人都可以修改它。
于 2013-08-21T00:43:00.333 回答