errors in my class....

This is a discussion on errors in my class.... within the C++ Programming forums, part of the General Programming Boards category; This is my first time using a class so I assume the errors are some simple mistake on my part. ...

  1. #1
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138

    errors in my class....

    This is my first time using a class so I assume the errors are some simple mistake on my part. I looked at the syntax in my books many times and mine looks right from what I see, so maybe someone more experienced knows my problem. Here are the errors:

    Compiling...
    rpgfight.cpp
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(29) : error C2143: syntax error : missing ';' before '.'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(29) : error C2501: 'rabbit' : missing storage-class or type specifiers
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(29) : error C2371: 'rabbit' : redefinition; different basic types
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(22) : see declaration of 'rabbit'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(29) : error C2143: syntax error : missing ';' before '.'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(30) : error C2143: syntax error : missing ';' before '.'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(30) : error C2501: 'rat' : missing storage-class or type specifiers
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(30) : error C2371: 'rat' : redefinition; different basic types
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(23) : see declaration of 'rat'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(30) : error C2143: syntax error : missing ';' before '.'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(31) : error C2143: syntax error : missing ';' before '.'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(31) : error C2501: 'sTroll' : missing storage-class or type specifiers
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(31) : error C2371: 'sTroll' : redefinition; different basic types
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(24) : see declaration of 'sTroll'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(31) : error C2143: syntax error : missing ';' before '.'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(32) : error C2143: syntax error : missing ';' before '.'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(32) : error C2501: 'lTroll' : missing storage-class or type specifiers
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(32) : error C2371: 'lTroll' : redefinition; different basic types
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(25) : see declaration of 'lTroll'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(32) : error C2143: syntax error : missing ';' before '.'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(33) : error C2143: syntax error : missing ';' before '.'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(33) : error C2501: 'dragon' : missing storage-class or type specifiers
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(33) : error C2371: 'dragon' : redefinition; different basic types
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(26) : see declaration of 'dragon'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(33) : error C2143: syntax error : missing ';' before '.'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(34) : error C2143: syntax error : missing ';' before '.'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(34) : error C2501: 'boss1' : missing storage-class or type specifiers
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(34) : error C2371: 'boss1' : redefinition; different basic types
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(27) : see declaration of 'boss1'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(34) : error C2143: syntax error : missing ';' before '.'
    Error executing cl.exe.

    rpgfight.exe - 24 error(s), 0 warning(s)


    and heres the code....
    Code:
    class cMob
    {
    public:
    	
    	int attack;
    	int itsAttackPower;
    private:
    
    };
    
    cMob rabbit;
    cMob rat;
    cMob sTroll;
    cMob lTroll;
    cMob dragon;
    cMob boss1;
    
    rabbit.itsAttackPower= 1;
    rat.itsAttackPower = 3;
    sTroll.itsAttackPower = 6;
    lTroll.itsAttackPower = 8;
    dragon.itsAttackPower = 12;
    boss1.itsAttackPower = 30;

    thanks!

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Because you didn't access the datamembers from within a function.

    Code:
    int main()
    {
      rabbit.itsAttackPower= 1;
      rat.itsAttackPower = 3;
      sTroll.itsAttackPower = 6;
      lTroll.itsAttackPower = 8;
      dragon.itsAttackPower = 12;
      boss1.itsAttackPower = 30;
      return 0;
    }
    You probably don't want those to be globals either.
    Last edited by Polymorphic OOP; 04-12-2003 at 05:36 PM.

  3. #3
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    ahh I see!! but I kind of do want them to be globals... Is there a way to make them globals?


    edit: im dumb....

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by o0obruceleeo0o
    ahh I see!! but I kind of do want them to be globals... WIll me putting them in public make them globals?
    No, I mean don't make the objects global. In other words, put cMob rabbit;
    cMob rat;
    cMob sTroll;
    cMob lTroll;
    cMob dragon;
    cMob boss1;
    in the function or class, etc. that uses them.

    Though it's usually considered poor practive to have your datamembers public. You usually want to make them private and have accessors for them.

  5. #5
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    ahh thanks so much!!! No more errors now

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    o0obruceleeo0o,

    Do you know what a constructor is? Use it to initialze the data members of your objects, and make your data members private:
    Code:
    class cMob
    {
    public:
    	//Constructor:
    	cMob(int attack_number, int attack_power)
    	{
    		attack = attack_number;
    		itsAttackPower = attack_power;
    	}
    	
    	int Get_Attack()
    	{
    		return attack;
    	}
    	
    	int Get_itsAttackPower()
    	{
    		return itsAttackPower;
    	}
    
    private:
    	int attack;
    	int itsAttackPower;
    
    };
    
    
    int main()
    { 
    	cMob rabbit(2, 1);
    	cMob rat(2, 3);
    	
    	cout<<rabbit.Get_Attack()<<endl;
    	cout<<rabbit.Get_itsAttackPower()<<endl;
    
    	cout<<rat.Get_Attack()<<endl;
    	cout<<rat.Get_itsAttackPower()<<endl;
    
    	return 0;
    
    }

  7. #7
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    7stud, ty for the tip, would I need to use a deconstructor? I am assuming that a constructer gives you acces to those functions and the deconstructer stops giving acces?

    Also im having another problem on this program.... I need to display the monsters name. I have a variable in class cMob called currentMob. I need to be able to display text saying "rabbit" if currentMob=rabbit; I have tried all the things I know to try, so if anyone has any tips it would be very much appreciated....

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    constructor does whatever is set in the constructor once, and only once, at the time of declaration of an instance of your class....destructor does its thing when you, uhh....little help here

  9. #9
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    when the object is destroyed (hence its name).

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "would I need to use a deconstructor? I am assuming that a constructer gives you acces to those functions and the deconstructer stops giving acces?"

    A constructor is called when you declare any object. Usually, you'll define the constructor to intialize your member variables like I did with the code in my previous post.

    A destructor is automatically called when your object goes out of scope or the program ends. Unless you need to do something special, like delete dynamically created memory or close open phone lines your object was using, you don't need to define a destructor youself--the default destructor will be called automatically.

    You should probably read a basic tutorial on classes before you attempt to write programs with them.
    Last edited by 7stud; 04-14-2003 at 03:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Class Errors (LNK2005)
    By NMZ in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2005, 02:52 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM

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