Jump to content

View more

Image of the Day

Boxes as reward for our ranking mode. ヾ(☆▽☆)
#indiedev #gamedev #gameart #screenshotsaturday https://t.co/ALF1InmM7K
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Need help with forward declarations C++

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
2 replies to this topic

#1 caiusg   Members   

110
Like
0Likes
Like

Posted 17 September 2013 - 06:08 PM

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


#2 smallGame   Members   

211
Like
1Likes
Like

Posted 17 September 2013 - 06:53 PM

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, 17 September 2013 - 06:54 PM.


#3 caiusg   Members   

110
Like
1Likes
Like

Posted 20 September 2013 - 06:55 AM

Thank you!

Your answer really helped a lot!






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.