Sign in to follow this  

Question about classes (error I don't get)

This topic is 2845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites

// variable declartion
weapon RustySword;
// statements
RustySword.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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :(

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I am mostly self-taught, but I'm actually taking formal courses at college right now. I'm not really learning very much from class, but we can't claim credit for engineering courses at my university. Having gone the self-taught route and then the formal route (textbook + instructor), I am in the strong opinion that textbooks are NOT a good idea until you have more exposure to programming.

Many of my classmates don't have a clue what's going on because any textbook worth its salt goes into detail the beginning programmers can't handle. What I would recommend are the following online tutorials, AFTER which you should pick up C textbook. I recommend a C textbook because it will cover very low-level (somewhat esoteric) information that most programmers are unaware of, and it will do a very good job because the C language is relatively small compared to the C++ language, allowing a more in-depth coverage. After that, proceeding to C++ is a walk in the park.

Here are the online resources that help(ed) me out:
http://www.cprogramming.com
http://www.cplusplus.com/doc/tutorial/
http://www.iu.hio.no/~mark/CTutorial/CTutorial.html
http://www.codeproject.com/
http://www.gamedev.net

These aren't a comprehensive list at all, but the first two were my mine source of information for a solid few days during a really boring summer...

As far as textbooks go, the number one text that I recommend is "Introduction to Computing Systems: From Bits & Gates to C & Beyond". Any version will be good.

Hope this helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by Brain me
After that, proceeding to C++ is a walk in the park.


I completely disagree with this. C and C++ are entirely different and the habits you would form learning C are terrible practice in C++. Whether or not a book is good for a person varies by person and book. I got a copy of C++ Primer Plus and it was very detailed, yet simple in its explanations.

Classes are the best way to go, as long as you supplement your studies. It is quite common for professors to pass on misinformation on proper programming habits for a particular language. The best way to supplement your studies are to practice, read and seek help from alternative sources. CodeGuru.com is an excellent place to have questions answered.

As far as free online sources go for C++, the C++ FAQ Lite is hands down the best place to go.

Share this post


Link to post
Share on other sites

This topic is 2845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this