• Advertisement
Sign in to follow this  

Need help with forward declarations C++

This topic is 1587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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()
{
}

Share this post


Link to post
Share on other sites
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.

Edited by smallGame

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement