[Solved] [C++] Classing question

Discussion about everything. New games, 3d math, development tips...
Post Reply
madoc
Posts: 32
Joined: Fri Feb 11, 2005 10:43 am

[Solved] [C++] Classing question

Post by madoc »

The goal:
I have a class CMain that creates the irrlicht device, I need to be able to pass that device to another class thats created within CMain. I remember doing this last time I used irrlicht I thought it had something to do with abstracts but a helpfull gentlemen on IRC told me how i should be going about it...but I can't get it to work. If someone can even tell me what this concept is called so I can google for it, I've read so much stuff about classes tonite trying to figure this out, but its never mentioned in the stuff I found.


Actual error:
Cmain.obj : error LNK2019: unresolved external symbol "public: __thiscall CProject::CProject(class CMain *)" (??0CProject@@QAE@PAVCmain@@@Z) referenced in function "public: unsigned short __thiscall Cmain::Go(void)" (?Go@Cmain@@QAEGXZ)
C:\Documents and Settings\madoc\My Documents\Visual Studio 2005\Projects\GUI_DEV\Debug\GUI_DEV.exe : fatal error LNK1120: 1 unresolved externals

Example code (stripped to code that matters):
And instead of CProject I used CWhatever to make it easier to read

Code: Select all

 
// Main.cpp
#include "CMain.h"
int main(void) {
  CMain main;
  main.go();
}

Code: Select all

//CMain.h
class Cwhatever;
class CMain {
private: 
    IrrlichtDevice *device;
public:  
    void go(); // This creates the device and assigns it 
    IrrlichtDevice *getDevice();
};

Code: Select all

//CMain.cpp
#include "CWhatever.h"
void CMain::go() {
     CWhatever thingy(this);
}

Code: Select all

//CWhatever.h
class CMain;
class CWhatever {
    public:
         CWhatever(Cmain *myparent);
};

Code: Select all

//CWhatever.cpp
#include "CMain.h"
CWhatever::CWhatever(Cmain *myparent) {
   myparent->getDevice()->DoSomeThing();

}
Last edited by madoc on Sun Feb 04, 2007 3:27 pm, edited 1 time in total.
CuteAlien
Admin
Posts: 10045
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

c++ is case sensitive.
You write sometimes Cwhatever and sometimes CWhatever.
Same with Cmain and CMain.
You are not allowed to do that.
RapchikProgrammer
Posts: 279
Joined: Fri Dec 24, 2004 6:37 pm

Post by RapchikProgrammer »

What you are writing can result an infinite includes! Because:

CMain needs CWhatever, to pass it the value!
And CWhatever is taking CMain so CMain is included!

My advise would be passing just a pointer to device! And btw i dont know the name of the method you are asking about!

And if you can use this method too:

Code: Select all

#include "CGame.h"

int main()
{
  CGame* game = new CGame;
  game->init();
  
  while (game->getDevice()->run())
  {
     if(game->getDevice()->isWindowActive())
        game->mainLoop();
   }
   
   return 0;
}

Code: Select all

#include "Globals.h"

#include "Player.h"
#include "Enemy.h"
#include "Level.h"
//other includes here too

class CGame
{
public:
  void init();
  void mainLoop();
  IrrlichtDevice* getDevice();

private:
  IrrlichtDevice* device;

  CPlayer* player;
  CEnemy* enemy;
  CLevel* level;
};

Code: Select all

#include "CGame.h"

void CGame::init()
{
  //init the game
  player = new CPlayer;
  enemy = new CEnemy;
  level = new CLevel;

  player->init(device);
  enemy->init(device);
  level->init(device);
}

 

Code: Select all

// Globals.h

#include "irrlicht"

//blah blah blah
madoc
Posts: 32
Joined: Fri Feb 11, 2005 10:43 am

Post by madoc »

What I posted was not the actual code, I know c++ is case sensitive, I just meant it to be an example, the actual code is like 10 pages, I've been working on this for awhile. It compiles fine, just a linker error, and I solve the infinate include problem the same way irrlicht does


#ifndef _CMAIN__
// the code for cmain
#endif

The second solution looks promising, using pointers instead, i'll try that
madoc
Posts: 32
Joined: Fri Feb 11, 2005 10:43 am

Post by madoc »

Whoops I just noticed in the real error message theres a capitalization difference.....which is odd, I just checked all the code and its all consistant, Cmain...I dont know where its getting CMain i even tried searching all files in the code for CMain with match case on, it didnt find any...hrm
madoc
Posts: 32
Joined: Fri Feb 11, 2005 10:43 am

Post by madoc »

OMG I'm an idiot, I guess it was just to late.
The actual problem was I hadnt done the implemention for the constructor for CProject, so of course it couldnt find the function I hadnt .
Thanx guys, I guess I was just working to late, little sleep and I found the error in 5min :)
Post Reply