2013-03-03 24 views
0

我编辑了我的原始消息,以便完全按照原样包含我的代码。对于麻烦抱歉。C++ Xcode,如何包含并声明我的对象

这就是我试图用一个指针,而我加入到我的项目类:

#ifndef MYAPP_DivGameLayer_h 
#define MYAPP_DivGameLayer_h 

// When you import this file, you import all the cocos2d classes 
#include "cocos2d.h" 
#include "Box2D.h" 
#include "DivBall.h"  

#define PTM_RATIO 32 
#define GRAVITY -10.0f 
#define VELOCITY_ITERS 8 
#define POSITION_ITERS 1 
#define BALLS_COUNT 2 

class DivGameLayer : public cocos2d::CCLayer { 

public: 

    ~DivGameLayer(); 
    DivGameLayer(); 

    // returns a Scene that contains the layer as the only child 
    static cocos2d::CCScene* scene(); 

    // functions for handling user input touches  
    virtual void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event); 

    // game update loop function 
    void update(float dt); 

private: 

    // CONSTANTS 
    float SCREEN_WIDTH; 
    float SCREEN_HEIGHT; 

    b2World* world;     // the game's world 
    DivBall* balls;     // balls 
    int next;      // pointer to next ball 

    // initialize the game's world 
    void initWorld(); 

    // initialize balls 
    void initBalls(); 
}; 
#endif 

,这是我DivBall头文件:

#ifndef MYAPP_DivBall_h 
#define MYAPP_DivBall_h 

#include "DivGameLayer.h" 

#define PATH "ball.png" 
#define DENSITY 0.25f 
#define FRICTION 0.1f 
#define RESTITUTION 0.7f 

class DivBall : public cocos2d::CCSprite { 

public: 

    ~DivBall(); 
    DivBall(DivGameLayer layer, b2World* world, cocos2d::CCPoint location, float Vx, float Vy, float omega); 

private: 

    DivBall();  
    b2Body* body; 

}; 

#endif 

这是DivBall .cpp:

#include "DivBall.h" 
#include "SimpleAudioEngine.h" 

using namespace cocos2d; 
using namespace CocosDenshion; 

// defualt CTOR 
DivBall::DivBall() 
{ 

} 

// CTOR 
DivBall::DivBall(DivGameLayer layer, b2World* world, CCPoint location, float Vx, float Vy, float omega) 
{ 
    CCSprite *sprite = CCSprite::create(PATH); 
    sprite->setPosition(CCPointMake(location.x, location.y)); 
    layer->addChild(sprite); 

    // Define the dynamic body. 
    b2BodyDef bodyDef; 
    bodyDef.type = b2_dynamicBody; 
    bodyDef.position.Set(location.x/PTM_RATIO, location.y/PTM_RATIO); 
    bodyDef.userData = sprite; 
    b2Body *body = world->CreateBody(&bodyDef); 

    // Define a circle shape for our dynamic body. 
    b2CircleShape circle; 
    circle.m_radius = (sprite->getContentSize().width/2.0f)/PTM_RATIO; 


    // Define the dynamic body fixture. 
    b2FixtureDef fixtureDef; 
    fixtureDef.shape = &circle; 
    fixtureDef.density = DENSITY; 
    fixtureDef.friction = FRICTION; 
    fixtureDef.restitution = RESTITUTION; 
    body->CreateFixture(&fixtureDef); 

    // set velocity 
    body->SetLinearVelocity(b2Vec2(Vx/PTM_RATIO, Vy/PTM_RATIO)); 
    body->SetAngularVelocity(omega); 
} 

// DCTOR 
DivBall::~DivBall() 
{ 
     // nothing... 
} 

我在GameLayer中得到一个编译错误:unknown type name Ball。

对不起,不清楚。

+0

,你能否告诉了'foo.h'头的内容? – 2013-03-03 13:02:50

+0

嗯,很可能是因为你的'foo.h'没有提供有效的'foo'类型? – us2012 2013-03-03 13:03:00

+2

@ us2012:我看到互相包含头文件是最有可能出现的问题 – 2013-03-03 13:04:06

回答

1

我创造了一些类我自己的,说富

不,你没有。您只创建了头文件和实现文件。当你在评论中提到:

没有什么foo.h中我只添加的文件:foo.h中和Foo.cpp中

好,那么编译器告诉你foo是一个未知键入名称,这很有意义,因为您尚未定义任何类型为foo的类型。您的foo.h标题为空。只需添加一个名为foo该头型,例如:

class foo 
{ 
    // Whatever... 
}; 

编辑:

更新后,现在更清楚是什么问题。在你的DivGameLayer类的定义中,你声明了DivBall*类型的成员变量。但是,DivBall.h标头未包含在定义了DivGameLayer的标头中。因此,在处理该类定义时,类型DivBall未知。

您应该添加一个前瞻性声明是:

class DivBall; // <== ADD THIS 

class DivGameLayer : public cocos2d::CCLayer 
{ 
    ... 
}; 
+0

我很抱歉。我没有解释我自己。请原谅我。我会在几分钟内获得代码。 – 2013-03-03 13:21:21

+0

@ user1541941:好的,完成后请在这里添加评论。 – 2013-03-03 13:22:39

+0

我在顶部更新了原始邮件。谢谢 – 2013-03-03 13:33:48