whats wrong here

This is a discussion on whats wrong here within the C++ Programming forums, part of the General Programming Boards category; i have the code Code: cPlayer MyPlayer; cout<<"Dagger Skill: %d"<<MyPlayer.GetValue( Dagger ); cin.ignore(); MyPlayer.SetValue(Dagger, 50); cout<<"Dagger Skill: %d"<<MyPlayer.GetValue( Dagger ); ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    176

    whats wrong here

    i have the code
    Code:
    	cPlayer MyPlayer;
    
    	cout<<"Dagger Skill: %d"<<MyPlayer.GetValue( Dagger );
                    cin.ignore();
    
    	MyPlayer.SetValue(Dagger, 50);
    
    	cout<<"Dagger Skill: %d"<<MyPlayer.GetValue( Dagger );
    and i get 3 parse errors and i think it has to do with the ( Dagger )
    part of the program, this is my attempt at making this
    Code:
    cPlayer MyPlayer;
    
    	printf("Dagger Skill: %d", MyPlayer.GetValue(DAGGER_SKILL));
    
    	printf("\n"); //Prints a New Line
    
    	MyPlayer.SetValue(DAGGER_SKILL, 50);
    
    	printf("Dagger Skill: %d", MyPlayer.GetValue(DAGGER_SKILL));
    into c++ (i have no knowlege of c)
    Last edited by sreetvert83; 09-20-2005 at 06:03 PM.
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    What does your class GetValue function look like?

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    cPlayer.h is this
    Code:
    #ifndef CPLAYER_H
    #define CPLAYER_H
    
    #include "definitions.h"
    
    class cPlayer
    {
    public:
    	cPlayer(void);
    
    	void SetValue(int SkillOrSpellID, int Value); //Sets the current value
    	int GetValue(int SkillOrSpellID); //Returns the current value
    private:
    	int SkillAndSpellValues[NUMBER_OF_SKILLS_AND_SPELLS];
    
    };
    
    #endif
    and my cPlayer.cpp looks like this
    Code:
    #include "cPlayer.h"
    
    cPlayer::cPlayer(void)
    {
    	int X;
    
    	for(X = 0; X < NUMBER_OF_SKILLS_AND_SPELLS; X++)
    	{
    		SkillAndSpellValues[X] = 0;
    	}
    	//Now all our values have been initialized to 0
    }
    
    void cPlayer::SetValue(int SkillOrSpellID, int Value)
    {
    	SkillAndSpellValues[SkillOrSpellID] = Value;
    }
    
    int cPlayer::GetValue(int SkillOrSpellID)
    {
    	return SkillAndSpellValues[SkillOrSpellID];
    }
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Get rid of the %d on the two lines:

    Code:
    cout<<"Dagger Skill: "<< MyPlayer.GetValue( Dagger );
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    ok, i did that and i still get the parse errors
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    What exactly are the parse errors and which lines are they on? And did you put:
    Code:
    using namespace std;
    //or
    using std::cin;
    using std::cout;
    At the top of the file that you use cin and cout in?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    Code:
    #include <iostream> 
    #include "cPlayer.h"
    using namespace std;
    
    int main()
    {
        cPlayer MyPlayer;
    
    	cout<<"Dagger Skill: "<<MyPlayer.GetValue( Dagger );
                    cin.ignore();
    
    	MyPlayer.SetValue(Dagger, 50);
    
    	cout<<"Dagger Skill: "<<MyPlayer.GetValue( Dagger );
    
    	return 0;
    }
    and i get parse errors on lines 9, 12, and14
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    How is Dagger defined in cPlayer.h?

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    its in defintions.h
    Code:
    #define NUMBER_OF_SKILLS_AND_SPELLS 100
     //weapon skills
    #define Dagger 0;
    #define Club 1;
    #define Thrown 2;
    #define Shield 3;
    #define Unarmed 4;
    #define ShortSword 5;
    #define Staff 6;
    #define Ambidextrous 7;
    #define Archery 8;
    #define Axe 9;
    #define LongSword 10;
    #define TwoHandedWeapons 11;
    #define Strong 12;
    #define Sap 13;
    #define Parry 14;
    #define SneakAttack 15;
    #define FavoredEnemy 16;
    #define Riposte 17;
    #define ExtraDamage 18;
    #define Slay 19;
                        
    //magic skills
                        
    #define BardicLearning 20;
    #define DruidLearning 21;
    #define PaladinLearning 22;
    #define RangerLearning 23;
    #define SorcererLearning 24;
    #define WizardLearning 25;
    #define ManaPoints 26;
    #define ReadMagic 27;
    #define Ritual 28;
    #define MassHeal 29;
    #define TurnRebukeUndead 30;
    #define Inspire Courage 31;
    #define Wild Shape 32;
    #define Empty Body 33;
    #define Detect Evil/Good 34;
    #define Lay on Hands 35;
    //single spells
    #define singleSpelllvlonetothree 36;
    #define singleSpelllvlfourtosix 37;
    #define singleSpelllvlseventonine 38;
    #define singleSpelllvlten 39;
    //full spell level
    #define Spelllvlonetothree 40;
    #define Spelllvlfourtosix 41;
    #define Spelllvlseventonine 42;
    #define Spelllvlten 43;
    //special abilitys
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  10. #10
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    You cant change the value of #define's. They are like const's, and this is why C++ has const's (among other reasons), so that you dont get some parse error thats not very explanatory of the problem when trying to change #defines, whereas trying to change consts does.

    Declare those values as simple ints (global) or int const (if you dont plan on changing them).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    look at the very last page of this its prety much the same questions
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  12. #12
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by sreetvert83
    look at the very last page of this its prety much the same questions
    So?

    The reply is right, you adjust the #define to the value you want.. but you cant adjust it at run-time, only before you compile the program.

    He also seems to be exampling in C, not C++, which is why he is using #define. If he was using C++ it would be like so:

    Code:
    int const NUMBER_OF_SKILLS_AND_SPELLS = 100
     //weapon skills
    int Dagger = 0;
    int const Club = 1;
    int const Thrown = 2;
    int const Shield = 3;
    int const Unarmed = 4;
    int const ShortSword = 5;
    int const Staff = 6;
    int const Ambidextrous = 7
    Since you want to change the value of Dagger, you should not have 'const' on it, therefor it is a global variable (which can be changed).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  13. #13
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You just demonstrated a reason why defines can be dangerous. When the compiler sees "Dagger" it replaces it with "0;":
    Code:
    #include <iostream> 
    #include "cPlayer.h"
    using namespace std;
    
    int main()
    {
        cPlayer MyPlayer;
    
    	cout<<"Dagger Skill: "<<MyPlayer.GetValue( 0; );
                    cin.ignore();
    
    	MyPlayer.SetValue(0;, 50);
    
    	cout<<"Dagger Skill: "<<MyPlayer.GetValue( 0; );
    
    	return 0;
    }
    The correct define should not have a semicolon at the end
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  14. #14
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    Quote Originally Posted by Dae
    So?

    The reply is right, you adjust the #define to the value you want.. but you cant adjust it at run-time, only before you compile the program.

    He also seems to be exampling in C, not C++, which is why he is using #define. If he was using C++ it would be like so:

    Code:
    int const NUMBER_OF_SKILLS_AND_SPELLS = 100
     //weapon skills
    int Dagger = 0;
    int const Club = 1;
    int const Thrown = 2;
    int const Shield = 3;
    int const Unarmed = 4;
    int const ShortSword = 5;
    int const Staff = 6;
    int const Ambidextrous = 7
    Since you want to change the value of Dagger, you should not have 'const' on it, therefor it is a global variable (which can be changed).
    if i dont want to use const, then why are all the declarations here int const?
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  15. #15
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    from what I can tell your logic was fine, you never tried to change the value of a #define. You tried to set the value of an array who's index was a #define (Dagger).

    With that said you need to remove the trailing semi colons from your defines, or change to int const as suggested.

    Code:
    if i dont want to use const, then why are all the declarations here int const?
    Dae omitted the const from Dagger.
    Not knowing how you use the rest of your defines he would not know whether you truely want the others to be const or not. From what I've seen of this and your other thread you do want them all to be const (including Dagger).
    Last edited by spydoor; 09-21-2005 at 09:49 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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