Need help with forward declarations C++

Started by
1 comment, last by caiusg 10 years, 7 months ago

Hi there!
I'm learning Cinder and C++ and I'm stuck with forward declarations.

I have two classes and I would like to access each other: brick uses track and track uses brick.

Can you help me to solve this? I spent many hours and I still can't find a solution. This almost works now, but I cannot access anything from track within brick.
I've also tried separating the implementation of brick.h to brick.cpp, but it broke everything.

main.cpp


#include "cinder/app/AppBasic.h"
#include "cinder/gl/gl.h"


#include "track.h"
#include "player.h"


using namespace ci;
using namespace ci::app;
using namespace std;


class game_ver_2App : public AppBasic
{
public:
void setup();
void update();
void draw();
void prepareSettings( Settings *settings );


private:
track mTrack;
};


void game_ver_2App::prepareSettings( Settings *settings )
{
}
void game_ver_2App::setup()
{
}


void game_ver_2App::update()
{
}


void game_ver_2App::draw()
{
}


CINDER_APP_BASIC( game_ver_2App, RendererGl )






brick.h


#pragma once


#include "track.h"


class track;


class brick
{
public:
brick();
void setTrack(track &theTrack);


private:
track *mTrack;
};


brick::brick()
{


}


void brick::setTrack(track &theTrack)
{
mTrack = &theTrack;
int b = mTrack->a;
}


track.h


#pragma once


#include <math.h>
#include "cinder/vector.h"
#include "cinder/BSpline.h"
#include "cinder/Rand.h"


#include "brick.h"


using namespace ci;
using namespace std;


class track
{


public:
track();
int a;
private:
vector<brick> brickPositions;
};


track::track()
{
}
Advertisement

Hi,

#include "track.h" // Include

class track; // forward declaration

You have to split in two files like you ve just tried. In brick.h you put the forward declaration and in brick.cpp you include brick.h and track.h. Then everything should work.

Thank you!

Your answer really helped a lot!

This topic is closed to new replies.

Advertisement