1.

Solve : C++ Header File?

Answer»

Im teaching myself how to program with c++ so im not so good at it, Im trying to get my header file to work, and the compiler comes up with an error saying
" multiple definition of `Var' "

So I think that I have a typo in the header somewhere, I looked multiple times and I didnt see anything, the #ifndef statements are correct, I included the header in 3 files and they are all written correctly

Here is the statements that I have, I left the bulk of the code out so i didnt take to much room but if you need it i can post it up.
Maybe you guys can find something i didnt.


#ifndef Header_H
#define Header_h
//Code
#endif


#include "Header.h"
//code


Thanks for your helpHave you defined 'Var' anywhere in your source? If yes, check if thats twice...
.
Just an advice- Use the once pragma insteada the inclusion guard you are using.I checked that also and i didnt see any, but to make sure it had to do with my header I put another varaible in the header that I know isnt used elsewhere in the program and the same error came up, but it also included the variable that I put in the header. and the error comes up the same number of times Ive included the header file in any other file. The error message repeats 3 times. Please post the complete[/u] code.Its a game of Battle Ship, not finished yet but it still doesnt work

/*****
Header
*****/
#ifndef BattleHeader_H
#define BattleHeader_h


int DisplayBoard(bool Board[][5]);//display function
int PlaceShips();//PlayerPlaces Ships


bool PlayerShips[5][5];//false is water, true is ship
bool PlayerStrikes[5][5];//false is 'havent hit' true is 'hit'

#endif

/*****
Main
*****/
#include
#include "BattleHeader.h"

using name espace std;

int main()
{
cout <<"Welcome to Battle Ship\n";
cout <<"The '1's on your display board are ships and '0's represent water.\n";
cout <<"The '1's on your Target Board are places you already fired at.\n";

//Main loop



for(int i=0; i < 5; ++i)
{
for(int j=0; j <5; ++j)
{
PlayerShips[ i][j] = false;
}
}

for(int i=0; i < 5; ++i)
{
for(int j=0; j <5; ++j)
{
PlayerStrikes[ i][j] = false;
}
}
PlaceShips();//Place Ships


//Secondary Loop

int MenuChoice;
cout <<"Main Menu\n";
cout <<"____________\n";
cout <<"[1]-Display My Ships\n";
cout <<"[2]-Display Where I Already Fire\n";
cout <<"[3]-Fire At The Enemy\n";

cout <<"[";
cin >> MenuChoice;
cout <<"]\n";

if (MenuChoice == 1)
{
DisplayBoard(PlayerShips);
}

if (MenuChoice == 2)
{
DisplayBoard(PlayerStrikes);
}

return 0;
}


/*****
Place Ships
*****/
#include "BattleHeader.h"
#include


using namespace std;

int PlaceShips()
{




cout <<"Enter the cordinates of the starting point of your ship starting at Row: '1' --- Column: '1' \n";
cout <<"You get 3 ships to place that are 3 spaces long\n";

int Column = 0;//Players Vars that tells where they want their ship to start
int Row = 0;

int ShipsPlaced = 0;//Counts ships placed


do{ // Main loop of function that iterates 3 times
ShipsPlaced += 1;


int loop;//used to loop back if their ship ends up off of the board
do{
DisplayBoard(PlayerShips);//Display the board for them

loop = 2;//declares it 2 so it doesnt loop back if not needed

cout <<"Enter the Row Number: ";
cin >> Row;
cout <<"Enter the Column Number: ";
cin >> Column;

Row -= 1;//So the player doesnt have to compensate for the natural ARRAY starting position
Column -= 1;


cout <<"Do you want your ship to go...\n";
cout <<"[1]Up\n";
cout <<"[2]Down\n";
cout <<"[3]Left\n";
cout <<"[4]Right\n";

int ShipDirection;
cin >> ShipDirection;



for (int i = 0; i < 3; ++ i)// places the first cordinate then tests which way they wanted their ship to go and iterates through the loop 3 times
{
PlayerShips[Row][Column] = true;

if (ShipDirection == 4)
{
Column += 1;
}

if (ShipDirection == 3)
{
Column -= 1;
}

if (ShipDirection == 2)
{
Row += 1;
}

if (ShipDirection == 1)
{
Row -= 1;
}

}


//if ship ends up off board then it loops back to TOP
if (Column > 5 || Column < 0 || Row > 5 || Row < 0)
{
cout <<"The Direction you choose puts the ship off the board\n";
cout <<"please choose again\n ";
loop = 1;
}

}while(loop == 1);

}while (ShipsPlaced < 3);

return 0;
}

/*****
Display
*****/

#include
#include "BattleHeader.h"

using namespace std;

int DisplayBoard(bool Board[][5])
{


for (int R = 0; R < 5; ++R)
{
for (int C = 0; C < 5; ++C)
{
cout << Board[R][C] << " ";
}
cout < }

return 0;

}




Since its not finished I put comments in place of some functoins and loops, but it should still be working


This is the errors I get with this code
multiple definition of `PlayerShips'
first defined here
multiple definition of `PlayerStrikes'
first defined here
multiple definition of `PlayerShips'
first defined here
multiple definition of `PlayerStrikes'
first defined here
ld returned 1 exit status
C:\Documents and Settings\home\My Documents\C++ ProgramFiles\MultipleSource Files\BattleShip\Makefile.win [Build Error] [BattleShip.exe] Error 1 Sorry, cant do much right now, on a mobile phone.
Using Dev-C++?Code: [Select]#ifndef Header_H
#define Header_h
//Code
#endif


#include "Header.h"
//code


why does the ifndef check for header_H (capital H) whereas you define "Header_h" within the block?

This is likely the issue since it will be including the same file multiple times.Yes, did you not try the

Code: [Select]#pragma once

as Ralph said?I tried changing the capital H to a lower ONE, and the lower h to a capital one and same error came up.


QUOTE from: Ashutosh32 on May 02, 2009, 12:22:44 AM

Yes, did you not try the

Code: [Select]#pragma once

as Ralph said?

Sorry, Im new to c++ so i haven't learned about #pragma yet
if you could explain how to use it in my case to help me Ill be greatly thankful


Quote from: Ashutosh32 on May 01, 2009, 02:32:52 PM
Sorry, cant do much right now, on a mobile phone.
Using Dev-C++?
and yes I am using Dev C++Remove the #ifndef mess, just add the line

Code: [Select]#pragma once

in the top of your source file. Quote from: Ashutosh32 on May 02, 2009, 11:22:06 PM
Remove the #ifndef mess, just add the line

Code: [Select]#pragma once

in the top of your source file.
thanks for the information but I ENDED up getting the same errorSo is there an error in my code or is it my compilerIf it's any help, I got the same errors trying to compile with Visual C++ 6.


Discussion

No Comment Found