scratch and error

This is a discussion on scratch and error within the C++ Programming forums, part of the General Programming Boards category; Starting from scratch on my charachter generator, I am taking one step at a time unfortuanatly my fist step is ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    scratch and error

    Starting from scratch on my charachter generator, I am taking one step at a time unfortuanatly my fist step is giving me problems. Here are the errors and code.

    In file included from mainrpg.cpp:3:
    rpg.h:22: parse error before `{' token
    rpg.h:36: parse error before `}' token

    Code:
    // rpg.h
    ifndef _RPG_H_
    #define _RPG_H_
    
    #include <string>
    
    class Character
    {
     public:   
            Character();
            ~Character();
            
            
            void showabilities();               
            
            
        private:
            // charachter attributes
            static const std::string ABILITYNAMES;
                    
            
            sruct attribs
            {
                    std::string name;
                    std::string profession;
                    std::string armortype;
                    std::string race;
                    int armorclass;
                    int abilities[6];
                    int hitpoints;
            };
                                                                                      
            void rollabilities();
                 
        
            
    };
    #endif
    Code:
    //rpg.cpp
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <time.h>
    using namespace std;
    
    const string Character::ABILITYNAMES;
    
    Character::Character()
    {
        Character::ABILITYNAMES={"Strength","Dexterity","Constitution",
                                          "Intelligence","Wisdom","Charisma"};
        rollabilities();
        showabilities();
    }
    
    Character::~Character()
    {}
            
            
    Character::showabilities()
    {
        for(int i=0;i<6;i++)
        {
            cout<<abilitynames[i]<<"\t"<<attribs.abilities[i]<<endl;
        }
    }
    
    Character::rollabilities()
    {
        const int LOW = 1;
        const int HIGH = 100;
        time_t seconds;
        //Get value from system clock and place in seconds variable.
        time(&seconds);
        //Convert seconds to a unsigned integer.
        srand((unsigned int) seconds);
        
        for(int i=0;i<6;i++)
        {
            attribs.abilites[i]=rand()%(HIGH-LOW+1)+LOW;
        }
    }
    Code:
    //mainrpg.cpp
    #include <iostream>
    #include <stdlib.h>
    #include "rpg.h"
    
    using namespace std;
    
    
    int main(int argc, char *argv[])
    {
      Character Player();
      system("PAUSE");	
      return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    sruct attribs
    It's always the simple things. I do believe you want "struct".

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Jeeze now a ton of errors popped up here they are and here is the code:

    rpg.cpp:14: syntax error before `::' token
    rpg.cpp:18: ISO C++ forbids declaration of `rollabilities' with no type
    rpg.cpp:19: ISO C++ forbids declaration of `showabilities' with no type
    rpg.cpp:20: parse error before `}' token
    rpg.cpp:22: syntax error before `::' token
    rpg.cpp:26: syntax error before `::' token

    rpg.cpp:28: parse error before `;' token
    rpg.cpp:28: syntax error before `++' token
    rpg.cpp:34: syntax error before `::' token
    rpg.cpp:40: ISO C++ forbids declaration of `time' with no type
    rpg.cpp:40: `int time' redeclared as different kind of symbol
    C:/Dev-Cpp/include/time.h:97: previous declaration of `time_t time(time_t*)'
    rpg.cpp:40: invalid conversion from `time_t*' to `int'

    rpg.cpp:42: ISO C++ forbids declaration of `srand' with no type
    rpg.cpp:42: `int srand' redeclared as different kind of symbol
    C:/Dev-Cpp/include/stdlib.h:362: previous declaration of `void srand(unsigned
    int)'
    rpg.cpp:44: parse error before `for'
    rpg.cpp:44: parse error before `;' token
    rpg.cpp:44: syntax error before `++' token

    Code:
    #ifndef _RPG_H_
    #define _RPG_H_
    
    
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <time.h>
    using namespace std;
    #include "rpg.h"
    
    
    
    Character::Character()
    {
        Character::ABILITYNAMES={"Strength","Dexterity","Constitution",
                                          "Intelligence","Wisdom","Charisma"};
        rollabilities();
        showabilities();
    }
    
    Character::~Character()
    {}
            
            
    Character::showabilities()
    {
        for(int i=0;i<6;i++)
        {
            cout<<abilitynames[i]<<"\t"<<attribs.abilities[i]<<endl;
        }
    }
    
    Character::rollabilities()
    {
        const int LOW = 1;
        const int HIGH = 100;
        time_t seconds;
        //Get value from system clock and place in seconds variable.
        time(&seconds);
        //Convert seconds to a unsigned integer.
        srand((unsigned int) seconds);
        
        for(int i=0;i<6;i++)
        {
            attribs.abilites[i]=rand()%(HIGH-LOW+1)+LOW;
        }
    }
    #endif
    Code:
    #ifndef _RPG_H_
    #define _RPG_H_
    
    #include <string>
    
    class Character
    {
     public:   
            Character();
            ~Character();
            
            
            void showabilities();               
            
            
        private:
            // charachter attributes
            static const std::string ABILITYNAMES;
                    
            
            struct attribs
            {
                    std::string name;
                    std::string profession;
                    std::string armortype;
                    std::string race;
                    int armorclass;
                    int abilities[6];
                    int hitpoints;
            };
                                                                                      
            void rollabilities();
                 
        
            
    };
    #endif
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include "rpg.h"
    
    using namespace std;
    const string Character::ABILITYNAMES;
    
    int main(int argc, char *argv[])
    {
      Character Player();
      system("PAUSE");	
      return 0;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Remove the #ifdef lines from your .c file. You are defineing _RPG_H_ and as such, your entire header is not included. It skips right to the #endif.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Removed the #ifndef from my rpg.cpp file but am still getting a ton of errors the very first one I can't even fix.

    rpg.cpp:12: parse error before `{' token
    rpg.cpp: At global scope:
    rpg.cpp:14: ISO C++ forbids declaration of `rollabilities' with no type
    rpg.cpp:15: ISO C++ forbids declaration of `showabilities' with no type
    rpg.cpp:16: parse error before `}' token

    rpg.cpp:23: ISO C++ forbids declaration of `showabilities' with no type
    rpg.cpp:23: prototype for `int Character::showabilities()' does not match any
    in class `Character'
    rpg.h:13: candidate is: void Character::showabilities()
    rpg.cpp:23: `int Character::showabilities()' and `void
    Character::showabilities()' cannot be overloaded
    rpg.cpp: In member function `int Character::showabilities()':
    rpg.cpp:26: `abilitynames' undeclared (first use this function)
    rpg.cpp:26: (Each undeclared identifier is reported only once for each function
    it appears in.)
    rpg.cpp:26: parse error before `.' token

    rpg.cpp: At global scope:
    rpg.cpp:31: ISO C++ forbids declaration of `rollabilities' with no type
    rpg.cpp:31: prototype for `int Character::rollabilities()' does not match any
    in class `Character'
    rpg.h:32: candidate is: void Character::rollabilities()
    rpg.cpp:31: `int Character::rollabilities()' and `void
    Character::rollabilities()' cannot be overloaded

    rpg.cpp: In member function `int Character::rollabilities()':
    rpg.cpp:42: parse error before `.' token

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     struct attribs
            {
                    std::string name;
                    std::string profession;
                    std::string armortype;
                    std::string race;
                    int armorclass;
                    int abilities[6];
                    int hitpoints;
            };
    All this does is define a structure type. It doesn't create an instance of this structure. That's just one problem I found at a glance. You'll likely want to fix that.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Fixed and declared a structure of type attribs
    Code:
    #ifndef _RPG_H_
    #define _RPG_H_
    
    #include <string>
    
    class Character
    {
     public:   
            Character();
            ~Character();
            
            
            void showabilities();               
            
            
        private:
            // charachter attributes
            static const std::string ABILITYNAMES;
                    
            
            struct attribs
            {
                    std::string name;
                    std::string profession;
                    std::string armortype;
                    std::string race;
                    int armorclass;
                    int abilities[6];
                    int hitpoints;
            };
            
            attribs playerstats;           
                                                                                                                                                     
            void rollabilities();
                 
        
            
    };
    #endif
    also fixed the corresponding instances in rpg.cpp to reflect the instance payerstats structure.

    Still getting pretty much the same errors though. Thanks for your help so far!

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Code:
    //main.cpp
    #include <iostream>
    #include <stdlib.h>
    #include "rpg.h"
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
      Character Player;
      system("PAUSE");	
      return 0;
    }
    
    
    //rpg.h
    #ifndef _RPG_H_
    #define _RPG_H_
    #include <string>
    
    struct attribs
    {
        std::string name;
        std::string profession;
        std::string armortype;
        std::string race;
        int armorclass;
        int abilities[6];
        int hitpoints;
    };
    
    class Character
    {
        public:
            Character();
            ~Character();
            static std::string ABILITYNAMES[];
        private:
            attribs playerstats;                                                                                                                                                            
            void rollabilities(); 
            void showabilities();
    };
    
    #endif
    
    //rpg.cpp
    #include <iostream>
    #include <ctime>
    #include <stdlib.h>
    #include <string>
    #include "rpg.h"
    using namespace std;
    
    Character::Character() 
    {
        rollabilities();
        showabilities();
    }
    
    Character::~Character() {}
    
    string Character::ABILITYNAMES[] = {"Strength", "Dexterity", "Constitution", 
                                            "Intelligence", "Wisdom", "Charisma"};
    
    void Character::showabilities()
    {
        for(int i=0;i<6;i++)
        {
            cout << ABILITYNAMES[i] << '\t' << playerstats.abilities[i] << endl;
        }
    }
    
    void Character::rollabilities()
    {
        const int LOW = 1;
        const int HIGH = 100;
        time_t seconds;
        //Get value from system clock and place in seconds variable.
        time(&seconds);
        //Convert seconds to a unsigned integer.
        srand((unsigned int) seconds);
        
        for(int i=0;i<6;i++)
        {               
            playerstats.abilities[i]=rand()%(HIGH-LOW+1)+LOW;
        }
    }
    Should work now..

    Edit: fixed a bug
    Last edited by laasunde; 07-30-2003 at 02:26 AM.

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Doesn't work.

    The main problem seems to be with the static string ABILITYNAMES.

    How do I implement this correctly and initialize it?

  10. #10
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Smile General Advice... Take smaller steps...

    Sorry, this isn't really an answer to your question. (I'm not at my compiler, and I've got to get back to work.)

    Take smaller steps. Debugging can get very difficult when the compiler is reporting dozens of errors.

    This is becomming one of my standard replies:
    Start small... Just write enough code so that it compiles. Add a little code at a time, test-compiling and test-running as you proceed. This way you will know right away where the problems are.

    It takes a bit of practice to learn how to sequence your program development so that it's testable as you go along. Usually, the user interface, or input/output is started first (but maybe not finalized) so you can see what your program is doing.

    Programming is difficult, and every programmer test-compiles and test-runs during development. Beginners should test-compile every line or two! More experienced programmers might only test-compile every "page" or so, depending on how familiar they are with the particular functions they are using, etc.
    Also, compilers can get confused... It "knows" that there is a syntax error, but it doesn't know what you were trying to do. This means that the error messages are often misleading. The error message will usually point-to the line with the problem (or the following line).

    Linker error messages can be more confusing, because during linking, the line numbers are no longer relevant, and the linker re-names variables and functions. (Name mangling?)

  11. #11
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    I agree with DougDbug. You should start small to learn how to figure out your own compiler errors.

    That said, laasunde's code does work if you change the includes to #include "rpg.h" instead of "rpg1.h" and add #include <time.h> (or even better <ctime>) back to the rpg.cpp file.

  12. #12
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Ok thanks for the input. I'll take your suggestions to heart and get this thing running.

  13. #13
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by jlou
    I agree with DougDbug. You should start small to learn how to figure out your own compiler errors.

    That said, laasunde's code does work if you change the includes to #include "rpg.h" instead of "rpg1.h" and add #include <time.h> (or even better <ctime>) back to the rpg.cpp file.
    Yeah, your right, my bad.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MySQL in C
    By eXist in forum C Programming
    Replies: 5
    Last Post: 05-13-2009, 11:26 AM
  2. Circular linked list to print last N lines of file
    By FortinsM3 in forum C Programming
    Replies: 7
    Last Post: 10-24-2008, 06:42 PM
  3. Extremely odd Serialization error
    By VirtualAce in forum Windows Programming
    Replies: 12
    Last Post: 01-02-2006, 01:55 PM
  4. Can Anyone Explain this Error? (OpenGL)
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 11-29-2005, 06:21 PM
  5. Float/double compiler error and TONS of questions!
    By musayume in forum C Programming
    Replies: 5
    Last Post: 10-24-2001, 02:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21