C++ using .h and .cpp file, having issues

Started by
6 comments, last by BennettSteele 12 years, 6 months ago
I am now switching from only using .h's to have seperate files included, but now im seperating it to .h's and .cpp's. ive done all of the files and such, but im wondering if i need to declare global variables from main.cpp into lets say, foo.cpp. if i dont, it will raise a error, and not continue. but now after fixing all the errors, it wont let me declare more than one variable (obviously), but i have to be able to use that name. so do i declare the class in the .h or what? here is a example:

Humanoid.h:


#ifndef Human_H_INCLUDED
#define Human_H_INCLUDED

class newHuman
{
public:
const char* name;
int TalkTimerMax,TalkTimer;
int Channelon;
int WalkTimer;
bool Alive,God;
int myX,myY,myZ;
float Speed;
float Health,Energy;
int WeaponHolding;
int Targetx,Targety;
int FollowingType;
int SoundType;
int favecolor;
int skin,hair;
bool isBurning;
void Init(float x,float y,float z,int Type,float speed,const char* name,int SoundType,int channel,int nskin);
void TakeDamage(int dam);
void Die();
void Speak(int what);
void Move();
void DoNow();
};

#endif // Human_H_INCLUDED




Humanoid.cpp:


#include "Humanoid.h"
#include "ChatData.h"
#include <math.h>
#include <cstdlib>
#include "App.h"
myApp APP;//already declared in main.c++
ChatData Chatty;//but will raise errors without being included

void newHuman::Init(float x,float y,float z,int Type,float speed,const char* name,int soundType,int channel,int nskin)
{
newHuman::Channelon=channel;
newHuman::Alive=true;
newHuman::God=false;
newHuman::WalkTimer=0;
newHuman::name=name;
newHuman::myX=x;
newHuman::myY=y;
newHuman::myZ=z;
newHuman::FollowingType=Type;
newHuman::Speed=speed;
newHuman::Targetx=x;
newHuman::Targety=y;
newHuman::isBurning=false;
newHuman::Health=100;
newHuman::Energy=100;
newHuman::Speed=speed;
newHuman::SoundType=SoundType;
newHuman::TalkTimerMax=rand()%100;
newHuman::TalkTimer=0;
newHuman::skin=nskin;
}

void newHuman::Speak(int what)
{
const char* say;
switch(what)
{
// case 0:say="Hello.";Mix_PlayChannel(newHuman::Channelon,IntAndSayToNoise(newHuman::SoundType,"Hello"),0);break;
//case 1:if(DistForm(newx,newy,TownFolk[n].x,TownFolk[n].y)<Dist){targx=TownFolk[n].x;targy=TownFolk[n].y;}}dirx=newx/targx;diry=newy/targy;break;//FOLLOW
//case 2:if(newHuman::WalkTimer>=70){newHuman::Targetx=(int)rand()/(int)RAND_MAX;newHuman::Targety=(int)rand()/(int)RAND_MAX;}targx=newHuman::Targetx;targy=Targety;dirx=newx/targx;diry=newy/targy;break;//WALK RANDOMLY
// default:say="...";Mix_PlayChannel(IntToChannel(newHuman::SoundType),IntAndSayToNoise(newHuman::SoundType,"..."),0);break;
}
Chatty.ChatStuff(say,newHuman::name);
}

void newHuman::Move()
{
float newx=newHuman::myX,newy=newHuman::myY;
int targx=100,targy=100;
int goingx=0,goingy=0;
float dirx=0,diry=0;
float Dist=APP.MAXSIGHT;//look distance
if(newHuman::FollowingType==3){Dist-=10;}
bool Seen=false,Walking=false;

switch(newHuman::FollowingType)
{
case 0:break;//DO NOTHING
//case 1:if(DistForm(newx,newy,TownFolk[n].x,TownFolk[n].y)<Dist){targx=TownFolk[n].x;targy=TownFolk[n].y;}}dirx=newx/targx;diry=newy/targy;break;//FOLLOW
//case 2:if(newHuman::WalkTimer>=70){newHuman::Targetx=(int)rand()/(int)RAND_MAX;newHuman::Targety=(int)rand()/(int)RAND_MAX;}targx=newHuman::Targetx;targy=Targety;dirx=newx/targx;diry=newy/targy;break;//WALK RANDOMLY
// case 3:targx=Game.myX;targy=Game.myY;if(DistForm(newx,newy,targx,targy)<Dist){dirx=targx-newx;diry=targy-newy;Seen=true;}break;//FOLLOW PLAYER
default:break;//DO NOTHING
}

if(dirx>0){goingx=1;Walking=true;}else if(dirx<0){goingx=-1;Walking=true;}
if(diry>0){goingy=1;Walking=true;}else if(diry<0){goingy=-1;Walking=true;}
// if(goingx==0 && goingy==0 && Seen){Game.TakeDamage(0.5);}

// if(goingx!=0 || goingy!=0){newHuman::Apperance=LookToBody(goingx,goingy,newHuman::Sheet);}

// POINT Where=MoveHere(newx,newy,newinx,newiny,goingx,goingy);

// if(Where.x!=0){newHuman::inx+=goingx*newHuman::Speed;}
// if(Where.y!=0){newHuman::iny+=goingy*newHuman::Speed;}

// if(newHuman::inx<0){newHuman::myX--;newHuman::inx=0.9;}else if(newHuman::inx>1){newHuman::myX++;newHuman::inx=0;}
// if(newHuman::iny<0){newHuman::myY--;newHuman::iny=0.9;}else if(newHuman::iny>1){newHuman::myY++;newHuman::iny=0;}

int NewWalkMax=rand()%10;
// if(newHuman::WalkTimer>=20+NewWalkMax && Walking){newHuman::WalkTimer=0;Mix_Volume(newHuman::Channelon,MIX_MAX_VOLUME/(Dist-(dirx+diry)/2));Mix_PlayChannel(newHuman::Channelon,TileToWalkSound(myMap.Environment[newHuman::myY][newHuman::myX]),0);}
newHuman::WalkTimer++;
}


void newHuman::Die()
{
newHuman::Alive=false;
newHuman::name="";
newHuman::myX=0;
newHuman::myY=0;
newHuman::myZ=0;
newHuman::FollowingType=0;
newHuman::Speed=0;
newHuman::Targetx=0;
newHuman::Targety=0;
newHuman::isBurning=false;
newHuman::Health=0;
newHuman::Speed=0;
newHuman::favecolor=0;
}

void newHuman::TakeDamage(int dam)
{
if(newHuman::God==false)
{
if(newHuman::Energy-dam>=0){newHuman::Energy-=dam;}
else if(newHuman::Energy-dam<=0 && newHuman::Energy>0)
{
int Leftover=newHuman::Energy-dam;
newHuman::Energy=0;
newHuman::Health-=Leftover;
}
else if(newHuman::Health-dam>=0){newHuman::Health-=dam;}
else if(newHuman::Health-dam<0){newHuman::Die();}
}
}

void newHuman::DoNow()
{
if(newHuman::Alive){newHuman::WalkTimer++;newHuman::Move();}
if(newHuman::Health<=0){newHuman::Die();}
if(newHuman::TalkTimer<newHuman::TalkTimerMax){TalkTimerMax++;}
// else if(DistForm(newHuman::myX,newHuman::myY,Game.myX,Game.myY)<15){newHuman::TalkTimer=0;if(newHuman::SoundType!=0){newHuman::Speak(-1);}}
}



here is the project:
http://www.mediafire.com/?x9kld6q9cfxwmau
the password is 22172
Advertisement
For your globals, you define them once (say in main.cpp) then let other classes / functions / etc know they exist by doing an 'extern MyApp app'. In essence, this tells the compiler that 'app' exists and the linker will do its magic and use the 'app' in main.cpp.
Have you read this article?
I just read it and sort of understand the problem. its the global variables i declare in main.cpp that all the other source files cant find. i have all the files, other than main.cpp in a lower directory... could that be the problem? i also added the project so you can edit whatever i did wrong.
bbobak told you to use extern to be able to use the global variables from other files. Just write a extern declaration whenever you need to use the global variable or if you find it easier you can put the extern declarations in a header file and include that file.
i tried using extern, like this:


#include "Functions.h"
#include "GameStats.h"
#include "ImageHolder.h"
#include "Map.h"
#include "App.h"
#include "ChatData.h"
extern ImageHolder Images;
extern GameStats Game;
extern Map myMap("lol");
extern myApp APP;
extern ChatData Chatty;



but there is a error, like this:


obj\Release\source\ScreenLogic.o:ScreenLogic.cpp:(.bss+0xa0): multiple definition of `APP'
obj\Release\main.o:main.cpp:(.bss+0x320): first defined here
collect2: ld returned 1 exit status

all over the place...
If you read what the error says it says you have defined APP both in ScreenLogic.cpp and in main.cpp. Replace one of the APP definitions with an extern declaration.
I see... it is going to take a while looking for all the places....

AND NOW IT WORKS! thank you so much! :D

This topic is closed to new replies.

Advertisement