2013-03-03 29 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