Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: C++ Header File  (Read 6079 times)

0 Members and 1 Guest are viewing this topic.

d-boy22

    Topic Starter


    Rookie
    C++ Header File
    « on: April 28, 2009, 06:22:29 PM »
    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 help

    Ralph Spencer

    • Guest
    Re: C++ Header File
    « Reply #1 on: April 29, 2009, 12:44:07 AM »
    Have you defined 'Var' anywhere in your source? If yes, check if thats twice... ;D
    .
    Just an advice- Use the once pragma insteada the inclusion guard you are using.

    d-boy22

      Topic Starter


      Rookie
      Re: C++ Header File
      « Reply #2 on: April 29, 2009, 05:25:59 AM »
      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. 

      Ashutosh32



        Intermediate

        Thanked: 1
        • Yes
        • Google
      • Experience: Experienced
      • OS: Windows XP
      Re: C++ Header File
      « Reply #3 on: April 29, 2009, 07:36:24 AM »
      Please post the complete[/u] code.

      d-boy22

        Topic Starter


        Rookie
        Re: C++ Header File
        « Reply #4 on: April 29, 2009, 03:00:31 PM »
        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 <iostream>
        #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 <iostream>


        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 <iostream>
        #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 <<endl;
            }
         
         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

        Ashutosh32



          Intermediate

          Thanked: 1
          • Yes
          • Google
        • Experience: Experienced
        • OS: Windows XP
        Re: C++ Header File
        « Reply #5 on: May 01, 2009, 02:32:52 PM »
        Sorry, cant do much right now, on a mobile phone.
        Using Dev-C++?

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: C++ Header File
        « Reply #6 on: May 01, 2009, 03:26:27 PM »
        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.
        I was trying to dereference Null Pointers before it was cool.

        Ashutosh32



          Intermediate

          Thanked: 1
          • Yes
          • Google
        • Experience: Experienced
        • OS: Windows XP
        Re: C++ Header File
        « Reply #7 on: May 02, 2009, 12:22:44 AM »
        Yes, did you not try the

        Code: [Select]
        #pragma once

        as Ralph said?

        d-boy22

          Topic Starter


          Rookie
          Re: C++ Header File
          « Reply #8 on: May 02, 2009, 10:29:31 AM »
          I tried changing the capital H to a lower one, and the lower h to a capital one and same error came up.


          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


          Sorry, cant do much right now, on a mobile phone.
          Using Dev-C++?
          and yes I am using Dev C++

          Ashutosh32



            Intermediate

            Thanked: 1
            • Yes
            • Google
          • Experience: Experienced
          • OS: Windows XP
          Re: C++ Header File
          « Reply #9 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.

          d-boy22

            Topic Starter


            Rookie
            Re: C++ Header File
            « Reply #10 on: May 05, 2009, 04:40:08 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 error

            d-boy22

              Topic Starter


              Rookie
              Re: C++ Header File
              « Reply #11 on: May 13, 2009, 04:16:03 PM »
              So is there an error in my code or is it my compiler

              BC_Programmer


                Mastermind
              • Typing is no substitute for thinking.
              • Thanked: 1140
                • Yes
                • Yes
                • BC-Programming.com
              • Certifications: List
              • Computer: Specs
              • Experience: Beginner
              • OS: Windows 11
              Re: C++ Header File
              « Reply #12 on: May 14, 2009, 08:49:47 AM »
              If it's any help, I got the same errors trying to compile with Visual C++ 6.
              I was trying to dereference Null Pointers before it was cool.