Problem with classes/ multiple inheritence
CodeOne

The story is: I have following files in a project.

Globals.h // global variables here

Gameobject.h // #include "globals.h" here ... PARENT CLASS
Gameobject.cpp // #include "Gameobject.h" here ...

A.h // #include "Gameobject.h" here ... CHILD class
A.cpp // #include "A.h" here ...

B.h // #include "Gameobject.h" here ... CHILD class
B.cpp // #include "B.h" here ...

main.cpp // #include ...all headers..

NOTE : Every header file has #pragma once

#SelectExpand
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
void Render(float *Cameraposition) as I want to draw my circle with its positions X & Y relative to Cameraposition[0], Cameraposition[1] respectively.

But its illegal . As the Render function in Gameobject.h has no arguments.

I used it in B.h
void Cameraupdate(float *Cameraposition)

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
And I dont want to pass its arguement. I tried many things here but either LNK2005 error OR LNK2001 comes up.

One of my failed idea was to include it in Globals.h : (Dont laugh I am learning :P )

#SelectExpand
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

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.

CodeOne

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

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.
Then define it in a single C or C++ file.

CodeOne

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
1>main.obj : error LNK2005: "float * Cameraposition" (?Cameraposition@@3PAMA) already defined in GameObject.obj

Edgar Reynaldo

extern float* and float[2] are not the same thing.

CodeOne

header:

extern float Cameraposition[];

Main.cpp:

float Cameraposition[2]= {0,0}; //inside main function

IN B.cpp I am using it like this:
void Rollingball::Cameraupdate()
{
Cameraposition[0] = ( x + BALL_WIDTH/2) -(WIDTH/2);
Cameraposition[1] = ( y + BALL_HEIGHT/2)-(330);

if(Cameraposition[0]<0)
Cameraposition[0]=0;
if(Cameraposition[1]<0)
Cameraposition[1]=0;

}

IN main function using it like this:
B->Cameraupdate();
al_identity_transform(&camera);
al_translate_transform(&camera,- Cameraposition[0],-Cameraposition[1]);
al_use_transform(&camera);

But i get

Error:
1>B.obj : error LNK2001: unresolved external symbol "float * Cameraposition" (?Cameraposition@@3PAMA)
1>C:\Users\Abhishek\Documents\Visual Studio 2010\Projects\oop\Debug\oop.exe : fatal error LNK1120: 1 unresolved externals

Thomas Fjellstrom
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

Aye! Thomas....You are my HERO.

Edgar and taron thanks to you guys too...

Thread #613758. Printed from Allegro.cc