|
Problem with classes/ multiple inheritence |
CodeOne
Member #15,442
December 2013
|
The story is: I have following files in a project. Globals.h // global variables here Gameobject.h // #include "globals.h" here ... PARENT CLASS A.h // #include "Gameobject.h" here ... CHILD class B.h // #include "Gameobject.h" here ... CHILD class main.cpp // #include ...all headers.. NOTE : Every header file has #pragma once 1int main(int argc, char **argv)
2{
3
4 float Cameraposition[2] = {0,0};
5.
6.
7//somewhere in code
8
9B->Cameraupdate(Cameraposition);
10 al_identity_transform(&camera);
11 al_translate_transform(&camera,-Cameraposition[0],-Cameraposition[1]);
12 al_use_transform(&camera);
13
14
15return0;
16}
Problem is : In Gameobject.h I have public function, void virtual render(); In A.h I have to use void render(); Now, in this render function of A.h i will have to pass an extra arguement But its illegal . As the Render function in Gameobject.h has no arguments. I used it in B.h Similarly I will face this extra argument issue in many couple of headers. Where and how should I place float * Cameraposition So that I wont have to pass it as extra arguement with functions like render(). In case i needed them somewhere inside a function. In short: I want to use the Cameraposition in Render(); of A.cpp One of my failed idea was to include it in Globals.h : (Dont laugh I am learning ) 1#pragma once
2
3float *Cameraposition;
4
5const int WIDTH = 800;
6const int HEIGHT = 400;
7const int BALL_RADIUS = 25;
8const int BALL_WIDTH = BALL_RADIUS *2;
9const int BALL_HEIGHT = BALL_RADIUS *2;
10const int NUM_O = 6;
11
12
13enum ID{PLAYER,GROUND,ENEMY,SQUARE,MISC};
14enum STATE{TITLE,PLAYING,LOST,WON};
Help me out!Please... |
Edgar Reynaldo
Major Reynaldo
May 2007
|
To share a variable among files, you need to declare it as extern in a single header and then define it once in a source file. Any file that includes the header will now have access to it. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
CodeOne
Member #15,442
December 2013
|
I did use extern float *Cameraposition too. But it gave me error LNK2005. I thought its because of #pragma once that the conflict is taking place. So i created a whole new header file x.h just to keep extern float *Cameraposition ; But Error again. While i use extern float *Cameraposition ,its definition is there in main function of main.cpp ...look at the code snippet of int main().ITS there.But still i am getting the problem. |
l j
Member #10,584
January 2009
|
This isn't a multiple inheritance problem. You can fix the issue by passing cameraposition to the constructor of the object that requires it. If you want to make it global, declare it extern in a header file.
|
CodeOne
Member #15,442
December 2013
|
Ok Taron.. extern float *Cameraposition; // declared in a header file Globals.h float Cameraposition[2]={0,0}; //definition is there in main.cpp inside my main function. I think, this is what you were suggesting. Or maybe I am getting it all wrong.Please correct me if I am. But this is still giving me error. I even tried and placed extern float *Cameraposition in new seperate header. As I thought error resulted from conflict of extern with #pragma once. But erroe still remained. ERROR: () when i keep extern in globals.h 1>B.obj : error LNK2005: "float * Cameraposition" (?Cameraposition@@3PAMA) already defined in GameObject.obj |
Edgar Reynaldo
Major Reynaldo
May 2007
|
extern float* and float[2] are not the same thing. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
CodeOne
Member #15,442
December 2013
|
header: extern float Cameraposition[]; Main.cpp: float Cameraposition[2]= {0,0}; //inside main function IN B.cpp I am using it like this: if(Cameraposition[0]<0) } IN main function using it like this: But i get Error: |
Thomas Fjellstrom
Member #476
June 2000
|
CodeOne said: float Cameraposition[2]= {0,0}; //inside main function That can't be inside the main function. You have to place it outside the main function. -- |
CodeOne
Member #15,442
December 2013
|
Aye! Thomas....You are my HERO. Edgar and taron thanks to you guys too... |
|