Question about classes (error I don't get)

Started by
10 comments, last by Raislin 14 years, 1 month ago
I am writing a small rpg (for fun and learning purposes). I have a Weapons.h file with my weapons class in it

class weapon
{

public:


int getWeaponName();
void setWeaponName(int name);

int getWeaponDamage();
void setWeaponDamage(int damage);

int getIntReq();
void setIntReq(int intReq);

int getIntBonus();
void setIntBonus(int intBonus);

int getStrReq();
void setStrReq(int strReq);

int getStrBonus();
void setStrBonus(int strBonus);

int getWeaponCost();
void setWeaponCost(int cost);

private:

int weaponName;
int weaponDamage;
int weaponIntReq;
int weaponStrReq;
int weaponIntBonus;
int weaponStrBonus;
int weaponCost;


};

int weapon::getWeaponName(){return weaponName;}
void weapon::setWeaponName(int name){weaponName = name;}

int weapon::getWeaponDamage(){return weaponDamage;}
void weapon::setWeaponDamage(int damage){weaponDamage = damage;}

int weapon::getIntReq(){return weaponIntReq;}
void weapon::setIntReq(int intReq){weaponIntReq = intReq;}

int weapon::getStrReq(){return weaponStrReq;}
void weapon::setStrReq(int strReq){ weaponStrReq = strReq;}

int weapon::getIntBonus(){return weaponIntBonus;}
void weapon::setIntBonus(int intBonus){weaponIntBonus = intBonus;}

int weapon::getStrBonus(){return weaponStrBonus;}
void weapon::setStrBonus(int strBonus){weaponStrBonus = strBonus;}

int weapon::getWeaponCost(){return weaponCost;}
void weapon::setWeaponCost(int cost){weaponCost = cost;}

weapon RustySword;
RustySword.setWeaponDamage(rand()%3 + 1);
RustySword.setIntReq(0);
RustySword.setStrReq(0);
RustySword.setIntBonus(0);
RustySword.setStrBonus(0);
RustySword.setWeaponCost(0);


(there are other weapons listed, but since the error is with all of them I am just posting one) I know it is linked correctly into my main file, but the error I get when I compile is: C:\*****\*****\*****\*****\Weapons.h|63|error: expected constructor, destructor, or type conversion before '.' token| I added my own constructor and destructor but it did not help. I am still very new to coding and trying to get better, so I thought I would make this project. It seems to have no problem with me declaring the weapon RustySword. The error seems to be with my setting the values (RustySword.setWhatever). And yes, Rand is seeded in my main file. In the code posted the + in the weapon damage did not show up for some reason. Thanks.
cout << "hi!n";
Advertisement
You can't call member functions on a global variable outside of a function.
// variable declartionweapon RustySword;// statementsRustySword.setWeaponDamage(rand()%3 + 1);RustySword.setIntReq(0);RustySword.setStrReq(0);RustySword.setIntBonus(0);RustySword.setStrBonus(0);RustySword.setWeaponCost(0);

The lines of code after "statements" cannot appear at file scope. They must be in some function.

Instead, give your weapon class a constructor that takes all the parameters as arguments:
class Weapon{public:   Weapon(      const std::string &name,      int damage, int cost,       int intelligenceRequirement, int intelligenceBonus,       int strengthRequirement, int strengthBonus)   :      // This syntax is called an "initializer list". Google it.      name(name),      damage(damage),      cost(cost),      intelligenceRequirement(intelligenceRequirement),      intelligenceBonus(intelligenceBonus),      strengthRequirement(strengthRequirement),      strengthBonus(strengthBonus)   {   }      // ...private:   // Don't prefix members with the class name, that is implied   // Instead, try to fully describe the variable's purpose.   // It is usually a bad idea to try and compress the variable name.   std::string name;   int damage;   int cost;   int intelligenceRequirement;   int intelligenceBonus;   int strengthRequirement;   int strengthBonus;};// Create a weapon with the given stats.Weapon RustySword("Rusty Sword", rand() % 3 + 1, 0, 0, 0, 0, 0);
Quote:Original post by rip-off
*** Source Snippet Removed ***
The lines of code after "statements" cannot appear at file scope. They must be in some function.

Instead, give your weapon class a constructor that takes all the parameters as arguments:
*** Source Snippet Removed ***


Thanks a ton, I will try this now.
cout << "hi!n";
Tried it and it works. The program compiles and runs no complaints and the code is now oh so much cleaner (and yes I did google it to understand what it is I was using)

I do have one other quick question though (I hope it is).

In my main file I want to give my player (Class is written) the RustySword. does:
Player Hero(5, 1, 1, 10, "RustySword", "");


work? My Player class is

class Player{     public:     Player(int health, int intelligence, int strength, int gold, const std::string weapon, const std::string armor):     health(health), intelligence(intelligence), strength(strength), gold(gold), weapon(weapon), armor(armor){}     private:            int health;            int intelligence;            int strength;            int gold;            std::string weapon;            std::string armor;};


I guess what I am trying to ask is how to assign the weapon I want to my Hero and will that code assign the weapon with all its stats to my Hero. Sorry for the newbish questions, really trying here :(
cout << "hi!n";
The easiest solution given your current architecture is to just have a setter for weapon. Take out the weapon assignment from the constructor unless other code depends on a player always having a valid weapon

class Player{public:     Player(int health, int intelligence, int strength, int gold):     health(health), intelligence(intelligence), strength(strength), gold(gold) {}     void setWeapon( const std::string &newWeapon );private:            int health;            int intelligence;            int strength;            int gold;            std::string weapon;            std::string armor;};inline void Player::setWeapon( const std::string &newWeapon ){     weapon = newWeapon;}
Quote:Original post by Palidine
The easiest solution given your current architecture is to just have a setter for weapon. Take out the weapon assignment from the constructor unless other code depends on a player always having a valid weapon

*** Source Snippet Removed ***


If I used that and set the weapon to Rusty Sword as in my Weapon class, would it then use those stats or when I set a new weapon do I have to define it then? That does not seem to make much sense...

Maybe another way to ask: When I set the weapon do I set it to the string name ("Rusty Sword") or the weapon name (RustySword)?

Player Hero(5, 1, 1, 10, "", "");
Hero.setWeapon(RustySword);

It seems to need the ""'s since that are listed in the Player class list. If I take them out and remove the ""'s (weapon/armor) I can then just use the setWeapon function and it will fill it in?

I am assuming so, but for future reference it would be good to have a definitive answer.

Many thanks to all.
cout << "hi!n";
When you initialize a variable, that "object" resides somewhere in memory. For your weapon class, each time you create a weapon

weapon RustySword;weapon RustyAxe;weapon RustyMace;


they each reside in a separate region of memory. The data within each weapon is independent of data in the other weapons.

When you pass integer parameters to the functions within your "weapon" class, you use those parameters to set data within that particular instance of the class. If you want to set data within a player class based on a particular instance of the weapon class, then you need to pass that instance, and not a string, which is itself different data having nothing to do with your variable name.

class Player{     public:          Player( int health, int intelligence, int strength, int gold );          setCurrentWeapon( weapon w );          // ...};//...void someFunctionThatCreatesWeaponAndAssignsToHero(){     weapon newGodSword( "God Sword", rand() % 3 + 5, 100, 10, 20, 20, 50 );     hero.setCurrentWeapon( newGodSword );}


It sounds like you might have tried to start running before learning to crawl. You really should learn more about variable declarations, initializations, scope, functions calls, the stack, and the heap before you dive into classes. You could probably cover the basics of these topics in an hour or two, but they are extremely important fundamentals.
Quote:Original post by Brain me
It sounds like you might have tried to start running before learning to crawl. You really should learn more about variable declarations, initializations, scope, functions calls, the stack, and the heap before you dive into classes. You could probably cover the basics of these topics in an hour or two, but they are extremely important fundamentals.


I think you are probably right. I am not going to argue and say I am good in all those areas, because I am not. Spending a few hours covering the basics will be good. Thanks for bringing me back down to earth...

I tried to learn to code abit awhile ago, got up to about this point then took several months off from it (life, etc). Came back and wanted to try again, and this project was to see were I was. Guess I forgot more then I thought I did.

Thanks to all who have posted help/advice in this thread for me. I am now off to get better. Out of curiosity, Brain me, is there any tutorials you would recommend for this stuff? I am teaching myself with no real help besides gamedev and a friend who is never around when I need him.

Thanks again.
cout << "hi!n";
Get yourself a book on C++ and perhaps a another one that acts as a good reference. You can usually get a good book on Amazon for pretty cheap. If you know this much, you'll probably make pretty quick progress.

This topic is closed to new replies.

Advertisement