Need help with forward declarations
Sep 17, 2013 at 11:55pm UTC
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
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()
{
}
Sep 18, 2013 at 12:25am UTC
brick.h should not include track.h. By including it you're not really using a forward declaration.
track.h shouldn't include brick.h. And you need to change the vector declaration to:
1 2 3 4 5 6 7 8 9
class track
{
public :
track();
int a;
private :
std::vector<brick* > brickPositions;
};
And don't put that using namespace stuff in header files.
Last edited on Sep 18, 2013 at 12:25am UTC
Sep 20, 2013 at 12:56pm UTC
Thank you for helping!
I managed to solve it!
Topic archived. No new replies allowed.