Windows 7
Dev C++ version 4.9.9.2

Hello I am fallowing the wrath lands game creator tutorial and I am having the following problem.

Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Proj\Nimph\Makefile.win"
Executing  make...
make.exe -f "C:\Dev-Cpp\Proj\Nimph\Makefile.win" all
g++.exe main.o battle.o town.o crossroads.o helper.o player.o  -o "Nimph.exe" -L"C:/Dev-Cpp/lib"  

crossroads.o(.bss+0x0):crossroads.cpp: multiple definition of `player'
town.o(.bss+0x0):town.cpp: first defined here
player.o(.bss+0x0):player.cpp: multiple definition of `player'
town.o(.bss+0x0):town.cpp: first defined here
collect2: ld returned 1 exit status

make.exe: *** [Nimph.exe] Error 1

Execution terminated

Here are the files:
global.h:

#ifndef GLOBAL_DEFEINE
#define GLOBAL_DEFINE

//prototypes

void wait();

bool crossroads();
bool town(PLAYER*);

#endif

library.h:

#include <iostream>

using namespace std;

#include "player.h"
#include "globals.h"

PLAYER player;

player.h:

//player
class PLAYER
{
     private:
          char name[32];
          int strength;
          int intelligence;
          int gold;
          int experience;
          int health;
          int maxHealth;
          int mana;
          int maxMana;
          int level;
     public:
          PLAYER();
          bool setName (char* newName);
          char* getName();
          void setStrength (int newStr);
          int getStrength();
          void setIntelligence (int newInt);
          int getIntelligence();
          void setGold (int newGold);
          void addGold (int amount);
          void spendGold (int amount);
          int getGold();
          void setExperiance (int newExperiance);
          void addExperiance (int amount);
          int getExperiance();
          void setHealth (int newHealth);
          void addHealth (int value);
          void loseHealth (int value);
          int getHealth();
          void setMaxHealth (int newMaxHealth);
          int getMaxHealth();
          void setMana (int newMana);
          void addMana (int amount);
          void loseMana(int amount);
          int getMana();
          void setMaxMana (int newMaxMana);
          int getMaxMana();
};

crossroads.cpp

//crossroads
#include "library.h"

//Global Variables
bool glcomplete  = false;
bool fcomplete = false;
bool scomplete = false;
bool gcomplete = false;
bool ccomplete = false;

//crossroads
bool crossroads()
{
    int choice = 0;

     while (choice != 10)
     {
          cout << "You stand at the corner of several roads going off in many directions.\n\n";
          cout << "There is a small town behind you full of shops and people.\n";
               
          

          cout << "Which path do you choose?\n";
          cout << "1: Town\n";
          cout << "2: Summoning Portal\n";
     
          if (!glcomplete)
          {
               cout << "3: Grasslands (QUEST)\n";
          }

          if (glcomplete && !fcomplete)
          {
               cout << "4: Forest Road (QUEST)\n";
          }

          if (fcomplete && !scomplete)
          {
               cout << "5: Swamp Road (QUEST)\n";
          }

          if (scomplete && !gcomplete)
          {
               cout << "6: Graveyard Road (QUEST)\n";
          }
     
          if (gcomplete && !ccomplete)
          {
               cout << "7: Castle Road (QUEST)\n";
          }

          cout << "10: Exit Game\n";
          cout << ">";

          cin >> choice;
          
          cout << endl;

          switch (choice)
          {
               case 1:
               {
                    cout << "You walk into the town.\n\n";
                    wait();
                    town(&player);
                    break;
               }
               case 2:
               {
                    //summoningportal()
                    break;
               }
               case 3:
               {
                    if (!gcomplete)
                    //grasslands()
                    break;
               }
               case 4:
               {
                    if (glcomplete && !fcomplete)
                    //forest()
                    break;
               }
               case 5:
               {
                    if (fcomplete && !scomplete)
                    //swamp()
                    break;
               }
               case 6:
               {
                    if (scomplete && !gcomplete)
                    //graveyard()
                    break;
               }
               case 7:
               {
                    if (gcomplete && !ccomplete)
                    //castle()
                    break;
               }
               default:
                    break;
          }
     }
     
     return true;
}

town.cpp

//town
#include "library.h"

//Town Functions
void inn();

bool town(PLAYER*)
{      
     int choice = 0;
     
     while (choice != 5)
     {
          cout << "You stand thn the middle of the town.\n";
          cout << "All around you there are peeople going about their daily lives.\n";
          cout << "To your left you spot an INN and to your right you spot the ARMORY and"
               << "\nITEM SHOP.\n\n";
          
          wait();

          cout << "What would you like to do?\n";
          cout << "1: Enter INN\n";
          cout << "2: Enter ARMORY\n";
          cout << "3: Enter ITEM SHOP\n";
          cout << "4: Check Stats\n";
          cout << "5: Leave Town\n";
          cout << ">";

          cin >> choice;
        
          cout << "\n";

          switch (choice)
          {
               case 1:
               {
                    //inn
                    break;
               }
               case 2:
               {
                    //armory
                    break;
               }
               case 3:
               {
                    //item shop
                    break;
               }
               case 4:
               {
                    //stats
                    break;
               }
               default:
                    break;
          }
     }
     return true;
}

void inn()
{
     cout << "You stand in a small almost vancent inn. Exept for tan old man standing behind"
          << "\na counter  there is no one else currently staying here.\n\n";
     cout << "You walk over to the old man. He grins, delighted to see a visitor.\n"
          << "Hello and welcome, would you like to saty in my inn for only 10 gold coins?";
     
     char answer[5];
     
     while (answer[0] != 'y' || answer[0] != 'n')
     {
          cout << "Stay? (10 gold) y/n\n";
          cin >> answer;
     }

     cout << endl;

     if (answer[0] == 'y')
     {
          if (player.getGold() > 10)
          {
               player.spendGold(10);
               cout << "You find a nice bed and fall; fast asleep.\n";
               
               wait();
               
               cout << "\n(HP and MP restored)/n\n";
               cout << "HP: " << player.getHealth() << "/" << player.getMaxHealth();
               cout << " MP: " << player.getMana() << "/" << player.getMaxMana();
               player.setHealth (player.getMaxHealth());
               player.setMana (player.getMaxMana());
               wait();
          }
          else
          {
               cout << "It looks like you don't have enough gold.\n";
               
               wait();
               
               answer[0] = 'n';
          }
     }
     
     if (answer[0] == 'n')
     {
          cout << "You turn around and walk out of the inn.\n";
     }
}

and player.cpp

#include "library.h"

PLAYER::PLAYER()
{
     strength = 1;
     intelligence = 1;
     gold = 0;
     experience = 0;
     health = 25;
     maxHealth = 25;
     mana = 0;
     maxMana = 0;
}
void PLAYER::addExperiance(int amount)
{
     setExperiance(experience + amount);
}

void PLAYER::addGold(int amount)
{
     setGold(gold + amount);
}

void PLAYER::addHealth(int value)
{
     setHealth(health + value);
}

void PLAYER::addMana(int amount)
{
     setMana(mana + amount);
}

int PLAYER::getExperiance()
{
     return experience;
}

int PLAYER::getGold()
{
     return gold;
}

int PLAYER::getHealth()
{
     return health;
}

int PLAYER::getMana()
{
     return mana;
}

int PLAYER::getIntelligence()
{
     return  intelligence;
}

int PLAYER::getMaxHealth()
{
     return maxHealth;
}

int PLAYER::getMaxMana()
{
     return maxMana;
}

char* PLAYER::getName()
{
     return name;
}

int PLAYER::getStrength()
{
     return strength;
}

void PLAYER::setExperiance(int newExperiance)
{
     experience = newExperiance;
     
     //check for level ups
}

void PLAYER::setGold(int newGold)
{
     gold = newGold;
}

void PLAYER::setHealth(int newHealth)
{
     health = newHealth;
     
     if (health > maxHealth)
          health = maxHealth;
     if (health < 0)
          health = 0;
}

void PLAYER::setIntelligence(int newInt)
{
     intelligence = newInt;
}

void PLAYER::setMana(int newMana)
{
     mana = newMana;
     
     if (mana > maxMana)
          mana = maxMana;
     if (mana < 0)
          mana = 0;
}

void PLAYER::setMaxHealth(int newMaxHealth)
{
     maxHealth = newMaxHealth;
}

void PLAYER::setMaxMana(int newMaxMana)
{
     maxMana = newMaxMana;
}

bool PLAYER::setName(char* newName)
{
     if (strlen(newName) == 0)
          return false;
     
     if (strlen(newName) > 32)
          return false;

     strcpy(name, newName);
     
     return true;
}

void PLAYER::setStrength(int newStr)
{
     strength = newStr;
}

void PLAYER::loseHealth(int amount)
{
     setHealth(health - amount);
}

void PLAYER::loseMana(int amount)
{
     setMana(mana - amount);
}

void PLAYER::spendGold(int amount)
{
     setGold(gold - amount);
}

If anyone could help me it would be greatly appreciated.

Hi Rallici :-)

You should wrap you header-files in 'include guards'... That will probably remove the problem.

#ifndef HEADER_INCLUDED
#define HEADER_INCLUDED

//Header content

#endif

Btw: The 'include guard' in Global.h has a typo. You miss-spell GLOBAL_DEFINE in the first line.

There are a few problems.

1)
You must
#include <cstring>
in player.cpp to use strlen

2) you have not given us main.cpp

3) You have declared wait() in globals.h, but not implemented it anywhere.

Once you fix those we can start to look at the multiple definition of 'player'. Multiple definition simply means that you have defined a variable named 'player' somewhere, then somewhere else in the same scope, you have tried to name something else 'player'.

Good luck,

Dave

Sorry for the late reply i have been busy getting ready to go to Iraq so i have not had to proper time to come back and update.

main.cpp

//main program file
#include "library.h"

int main()
{
     // getneral welcome screen
     cout << "\n\n\t\t\tWelcome to WRATH LANDS\n";
     cout << "\t\t      programmed by Mark Tryoyer\n\n";
     
     cout << "An Evil Demon creature known as the Wrath Lord has taken control of the world.\n"
          << "He controls a vast army of evil creatures that kill  poeple for fun. You must\n"
          << "rise agianst the Wrath Lord and kill all of his minions.\n\n";
        
     wait();

     crossroads();
     
     cout << "\n\nThank you for playing WRATH LANDS! Please play agian soon\n\n";

     system("pause");
}

helper.cpp

//helper functions

void wait()
{
     cout << "Press Enter to continue";

     cin.ignore(1);
     cin.get();
     
     cout << endl;
}

updated library.h file

#include <iostream>
#ifndef PLAYER_H
#define PLAYER_H
#ifndef GLOBALS_H
#define GLOBALS_H

using namespace std;

#include "player.h"
#include "globals.h"
#include "helper.cpp"

#endif
#endif

This is still a crazy amount of code for us to go through. You should be able to get all of these classes (cpp and h files) down to only a few lines. (We don't need to the content, only the structure to get the compiler errors you are seeing).

You've misinterpreted what daviddoria has said. Include guards only go in the include file of the file you wish to guard.

So

#ifndef PLAYER_H
#define PLAYER_H
/* Your header code here */
#endif

You don't need that "define" anywhere else in your app :)

@ daviddoria This is a video tutorial that i have been following that is located at http://www.rdxgames.net/projects/wrathlands/index.html so i have not come up with this at all and I do know it can be compressed.

@ ketsuekiame thank you for the clarification and i will attempted to do that and see what i get.

EDIT: Well that got rid of my multiple definitions but now if you look at the bool town(PLAYER* player) in the video tutorial this works for him do declare player as an instance however when i attempted this it points me to the inn of the town and when it checks the players gold it says that it is not declared. it also dose this for every other player.public after this.

Here are the two lines for quicker reference.

bool town(PLAYER* player).....
..... if (answer[0] == 'y')
     {
          if (player.getGold() > 10)
          {
               player.spendGold(10);....

Edited 6 Years Ago by Rallici: n/a

This article has been dead for over six months. Start a new discussion instead.