2 Replies - 7560 Views - Last Post: 10 February 2010 - 12:19 AM Rate Topic: -----

#1 vagrantdream   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-February 10

Multiple definition error with Header

Posted 10 February 2010 - 12:00 AM

Hello all!

I am using the dev-c++ ide, as a caveat.

So, basically my friend and I are fairly new to C++ and programming in general outside of HTML/markup stuff, and while we're learning pretty quickly we bumped into a particular problem that we have been trying to solve but so far no luck.

As an ongoing project to implement new facets of C++ in different ways, we're developing this little text-based console game. In it, we need variables that will be available throughout the program once defined.

At first we had one looooong source file that had multiple functions which each housed different 'paths' within the game - different professions, different story, and so on.

Well we started learning about including multiple files in one program so we broke the functions up into individual source files, partially just as an exercise and also so that we could more easily work on individual source files independantly without having to scroll through an increasingly long single file.

We learned about global variables and referencing them with extern, and that worked for basic elements, but now we want to start implementing stats, etc., without having to redefine the variables in every file, and without having to include a long list of extern references either.

The answer seemed to be the inclusion of a header file. So that's that we did:

#ifndef Globals_H
#define Globals_H

#include <string>

using namespace std;

string name;
short int classHpMod;
short int str, dex, con, wiz, cha, intl;
short int hp = (con * classHpMod);
short int mana = (wiz * intl);
short int atk = (str + dex), dmg, defns;
short int exp;


It's a variation on basic d20 style stat handling, not that it matters. The problem is that when #included in the other .cpp files like so:

#include <iostream>
#include <string>

void fighter ();
void wizard ();
void thief ();

using namespace std;

#include "Globals.H"

int main(int argc, char *argv[])
    string choice1;
//this runs fine when the variables are declared normally

I get an error "multiple definition of 'name' First defined here", 'name' being the name of one variable, I get the same for every variable declared in the header. These apparently crop up in the makefile.win, as this is where the error supposedly is.

Now, I know that there is probably a better way to accomplish this, and if the header does work it might not do exactly what I want, but right now we're just trying to implement it to see how it works and what it can do.

I had read that you can declare and optionally initialize variables within a header, that you don't have to define them in a source file - although I might have misinterpreted what was being explained there - and I followed the syntax as well as I was able to when creating this header file. However, still I get these errors. Can anyone clue me in to what I am doing wrong?


Is This A Good Question/Topic? 0
  • +

Replies To: Multiple definition error with Header

#2 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 547
  • View blog
  • Posts: 1,420
  • Joined: 22-August 09

Re: Multiple definition error with Header

Posted 10 February 2010 - 12:12 AM

Try to avoid putting #include file statements into header files. You have defined #include <string> in the header file and in the main source file. Thats the reason for the error. The ordering of header files becomes very important as you introduce more of them.

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: Multiple definition error with Header

Posted 10 February 2010 - 12:19 AM

1. Avoid multiple inclusions of header files ( you have same file included in .h and .c). Mostly try to have header inclusions under conditional macros which will make sure that no header file is included multiple times.
yeah, sort of Martyn.Rae's explanation which is better one I guess. :)

2. Avoid having variable declarations in header files. If a header file has variables declared in it, and this file gets included twice, it may create naming conflicts. Have variables in related .c files as global instead and extern them in .h

I hope this will help you.

This post has been edited by AmitTheInfinity: 10 February 2010 - 12:20 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1