Simon Ulsøe Dyrn
Simon Ulsøe Dyrn

Reputation: 11

How to use variables and functions declared in a header in C++

First of all this might be messy, because I'm pretty new to programming in general.
Well I'm making a rpg game and I want all my weapons to be in a file called Weapons.cpp.
I made it so that I got all of my global variables such as "weaponDamage" and "weaponName" in a header file called common.h so that I can access and manipulate those variables from both my main and my .cpp file. But the problem is that it can't seem to find those variables and functions in my header.

Here's some code:

common.h:

#include <string>  
#ifndef COMMON_H_INCLUDED  
#define COMMON_H_INCLUDED  

//global variables  
extern int pureDamage = 0;  
extern int pureHealth = 0;  
extern int weaponDamage;  
extern int armorDefense;  
extern int totalDamage = pureDamage + weaponDamage;  
extern int totalHealth = pureHealth + armorDefense;  
extern int totalLuck;  
extern string starsign;  
extern string weaponName;  

//all weapons  

void weaponSwordIron();  
void weaponSwordGold();  
void weaponSwordSwordOfTheHeavens();  
void weaponBowSimple();  
void weaponBowLongBow();  
void weaponBowThunder();  
void weaponStaffStaffOfFlames();  
void weaponStaffStaffOfLightning();  
void weaponStaffStaffOfAssKicking();  

#endif // COMMON_H_INCLUDED  

Weapons.cpp:

#include <iostream>  
#include <string>  
#include <common.h>  


using namespace std;

void weaponSwordIron()
{
    int weaponDamage = 5;
    string weaponName = "Iron Sword";
}
void weaponSwordGold()
{
    int weaponDamage = 8;
    string weaponName = "Gold Sword";
}
void weaponSwordSwordOfTheHeavens()
{
    int weaponDamage = 15;
    string weaponName = "Sword Of The Heavens";
}
void weaponBowSimple()
{
    int weaponDamage = 5;
    string weaponName = "Simple Bow";
}
void weaponBowLongBow()
{
    int weaponDamage = 8;
    string weaponName = "Long Bow";
}
void weaponBowThunder()
{
    int weaponDamage = 15;
    string weaponName = "Thunder Bow";
}
void weaponStaffStaffOfFlames()
{
    int weaponDamage = 5;
    string weaponName = "Staff Of Flames";
}
void weaponStaffStaffOfLightning()
{
    int weaponDamage = 8;
    string weaponName = "Staff Of Lightning";
}
void weaponStaffStaffOfAssKicking()
{
    int weaponDamage = 15;
    string weaponName = "Staff Of Ass Kicking";
} 

and a little piece of my main, the function called GiveWeapon():

void GiveWeapon()
{

    system("cls");
    if (starsign == "mage")
    {
        weaponSwordIron();
        cout << weaponDamage;
        cout << weaponName;
    }
    else if (starsign == "warrior")
    {
        weaponBowSimple();
    }
    else if (starsign == "archer")
    {
        weaponStaffStaffOfFlames();
    }
    else
    {
        ChooseStarsign();
    }
    AssignAttributes();
}  

And yes I did remember to include common.h
Now the error my IDE Code::Blocks comes up with is : error: common.h: no such file or directory
I don't know why it comes up with that so please help.

Sorry for the long post and thanks in advance.

Upvotes: 1

Views: 1042

Answers (5)

Greg Kikola
Greg Kikola

Reputation: 573

In addition to the header location issues and local variable shadowing that others have mentioned, I think you're also going to run into problems with multiple definition errors:

extern int pureDamage = 0;

The initializer on this declaration overrides the extern, so this variable will be not only declared but also defined in any compilation unit that includes this header. Your linker will complain.

When you really want to use globals across multiple files you'll need to define and initialize the variable once in a source file and declare it extern (with no initializer) in the header file. const variables are an exception to this, however.

Upvotes: 0

Pavel Ognev
Pavel Ognev

Reputation: 982

Where is your "common.h" placed? You must specify relative path to them from folder with the "Weapons.cpp".

At second, you define local variables in your functions. Your Weapons.cpp file must look like:

#include <iostream>
#include <string>
#include "relative/path/to/common.h"

//global variables
int pureDamage = 0;
int pureHealth = 0;
int weaponDamage;
int armorDefense;
int totalDamage = pureDamage + weaponDamage;
int totalHealth = pureHealth + armorDefense;
int totalLuck;
string starsign;
string weaponName;

using namespace std;

void weaponSwordIron()
{
    weaponDamage = 5;
    weaponName = "Iron Sword";
}
void weaponSwordGold()
{
    weaponDamage = 8;
    weaponName = "Gold Sword";
}
...

Upvotes: 0

elyashiv
elyashiv

Reputation: 3691

weopens.cpp is mostly non-sense.

i tried compiling the following:

void a() {
 int b = 5; }

int main()
{
 a();
 return 0;
}

and g++ didn't like the idea.

Upvotes: 0

Daniel
Daniel

Reputation: 31599

Use "common.h" instead of <common.h>. The angled ones are for library files.

Upvotes: 2

AbstractProblemFactory
AbstractProblemFactory

Reputation: 9811

You should use "common.h" not < common.h>

Upvotes: 0

Related Questions