Thread: varible initiliazation error's

  1. #1
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26

    varible initiliazation error's

    hi ,

    have iniatilised some varibles in a seperate header fill so that they are global,but when i go to change them in main() i keep getting errors.
    here's the header file:
    Code:
    #ifndef GoblinGlobal_h
    #define GoblinGlobal_h
    
    int paladinHealth=0;
    int paladinIni=0;
    int paladinStr=0;
    int paladinAc=0;
    
    int fighterHealth=0;
    int fighterIni=0;
    int fighterStr=0;
    int fighterAc=0;
    
    #endif
    and heres the part in main,its a part of a switch case:
    Code:
    switch(charType){
    		case 1:
    			cout<<"press enter for Paladins stats"<<endl;
    			cin.get();
    			int paladinHealth=76+dieRoll(6,4);
    			int paladinIni=dieRoll(6,3);
    			int paladinStr=76+dieRoll(6,3);
    			int paladinAc=dieRoll(6,3);
                cout<<"Paladin health="<<paladinHealth<<endl;
    			cout<<"Paladin iniative="<<paladinIni<<endl;
    			cout<<"Paladin strength="<<paladinStr<<endl;
    			cout<<"Paladin Armour="<<paladinAc<<endl;
    			
    			break;
    
    		case 2:
    			cout<<"press enter for Fighters stats"<<endl;
    			cin.get();
    			
                int fighterHealth=76+dieRoll(6,4);
    			int fighterIni=dieRoll(6,3);
    			int fighterStr=76+dieRoll(6,3);
    			int fighterAc=dieRoll(6,3);
    			break;
    	}
    nd finially here's the error log:
    Code:
    c:\users\ghetto smurf\documents\c++ source\goblinsmacker\goblinsmacker\goblinsmacker.cpp(23) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
    c:\users\ghetto smurf\documents\c++ source\goblinsmacker\goblinsmacker\goblinsmacker.cpp(52) : error C2360: initialization of 'paladinAc' is skipped by 'case' label
            c:\users\ghetto smurf\documents\c++ source\goblinsmacker\goblinsmacker\goblinsmacker.cpp(44) : see declaration of 'paladinAc'
    c:\users\ghetto smurf\documents\c++ source\goblinsmacker\goblinsmacker\goblinsmacker.cpp(52) : error C2360: initialization of 'paladinStr' is skipped by 'case' label
            c:\users\ghetto smurf\documents\c++ source\goblinsmacker\goblinsmacker\goblinsmacker.cpp(43) : see declaration of 'paladinStr'
    c:\users\ghetto smurf\documents\c++ source\goblinsmacker\goblinsmacker\goblinsmacker.cpp(52) : error C2360: initialization of 'paladinIni' is skipped by 'case' label
            c:\users\ghetto smurf\documents\c++ source\goblinsmacker\goblinsmacker\goblinsmacker.cpp(42) : see declaration of 'paladinIni'
    c:\users\ghetto smurf\documents\c++ source\goblinsmacker\goblinsmacker\goblinsmacker.cpp(52) : error C2360: initialization of 'paladinHealth' is skipped by 'case' label
            c:\users\ghetto smurf\documents\c++ source\goblinsmacker\goblinsmacker\goblinsmacker.cpp(41) : see declaration of 'paladinHealth'
    Build log was saved at "file://c:\Users\ghetto smurf\Documents\C++ SOURCE\GoblinSmacker\GoblinSmacker\Debug\BuildLog.htm"
    GoblinSmacker - 4 error(s), 1 warning(s)
    ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

    I know the prog is naff,just trying to put together all i have read(and hopefully learnt) before i move on,any help appreciated.
    I'm using ms visual 2005 express

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Why are you redeclaring your int variables inside your switch/case ?

    Also, your header file (bad that it is) should contain only declarations, not initialisations.

    // This in the header file
    int paladinHealth;

    // This as a global variable in ONE source file
    int paladinHealth=0;

    How about paladin.cpp,.h and fighter.cpp,.h files ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26
    so i should have a paladin.h file where i should just declare:
    Code:
    int paladinHealth;
    and then in paladin.cpp i should :
    Code:
    #include"paladin.h"
    
    int main()
    {
        paladinHealth=76+dieRoll(6,3);
    }
    I left out most of the code.I shouldnt do this again:
    Code:
    int main()
    {
    int paladinHealth=76+dieroll(6,3)
    }
    I think im just confusing myself bear with me im new to c++

    labmouse

  4. #4
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26
    done as you said salem and the prog is working fine.thanks.

    As a rule of thumb in header files i should only:

    1)only declare varibles if they are global,not initialise them
    2)only insert function prototypes not definitions.

    just one question.What do you mean by havng a paladin.cpp and fighter.cpp files?I have just put both char generation's into main.

    Thanks anyhow

    Labmouse

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As a hint, I would suggest that you declare the variables in the header file as "extern int blah;"

    This will ensure that you get a compiler error if you then FORGET to introduce the variable in your single .c file that holds the actual variables and their initialization [of course, global variables are always guaranteed to be zero - so you don't need to say "int blah = 0;" - but in cases where you want to initialize to a different value, that will be helpful].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's not a good idea to put any type of variable, declaration or definition into a header file. If more than one .cpp file includes that header, you'll get identical symbols linking error. Instead, declare your variables inside a .cpp file and if you want other .cpp files to access those variables, declare them as "extern" in the header file, telling the compiler they exist, but not in the current .cpp file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  2. Winsock compilation errors
    By jmd15 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-03-2005, 08:00 AM
  3. Unknown Errors in simple program
    By neandrake in forum C++ Programming
    Replies: 16
    Last Post: 04-06-2004, 02:57 PM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. Help me with these errors... :-(
    By major_small in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2003, 08:18 PM