Amit Farkash
Amit Farkash

Reputation: 329

C++ Xcode, how to include and declare my object

I edited my original message to include my code exactly as it is. Sorry for the troubles.

this is where I'm trying to use a pointer to a class which I added to my project:

#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

and this is my DivBall header file:

#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

this is 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...
}

and I get a compilation error in GameLayer: unknown type name Ball.

Sorry for not being clear.

Upvotes: 0

Views: 641

Answers (1)

Andy Prowl
Andy Prowl

Reputation: 126502

I created some class of my own, say foo

No, you haven't. You only created a header file and and implementation file. As you mention in the comments:

there's nothing in foo.h I just added the files: foo.h and foo.cpp

Well, then the compiler is telling you foo is an unknown type name, and that makes sense since you haven't defined any type named foo. Your foo.h header is empty. Just add a type named foo to that header, for instance:

class foo
{
    // Whatever...
};

EDIT:

After the update, it is now clearer what the problem is. In the definition of your DivGameLayer class, you declare a member variable of type DivBall*. However, the DivBall.h header is not included by the header where DivGameLayer is defined. Therefore, while processing that class definition, the type DivBall is unknown.

You should add a forward-declaration for it:

class DivBall; // <== ADD THIS

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

Upvotes: 1

Related Questions