Sign in to follow this  
finky45

error which has me stumped

Recommended Posts

I'm trying to pass a cArmor object in a cPlayer object at the constructor... cPlayer header:
class cPlayer
{
public:
       cPlayer (string, cArmor);
//       ~cPlayer();
       
       int getHealth() const;
       int getMoney() const;
       int getEnergy() const;
       string getName() const;
       void setHealth(int);
       void addHealth(int);
       void setMoney(int);
       void takeMoney(int);
       void showStats();
       void addEnergy(int);
private:
       int signed itsHealth, itsEnergy, itsMaxHealth, itsMaxEnergy;
       int itsMoney;
       string itsName;
       cArmor itsArmor; //
};
This is my cPlayer constructor, I get the error here:
cPlayer::cPlayer(string name, cArmor armor)
{
     itsMaxHealth=100;
     itsMaxEnergy=100;
     itsHealth=100;
     itsMoney=5000;
     itsName=name;
     itsEnergy=100;
     itsArmor=armor; //i want to set this to a cArmor object
}
cArmor.ccp:
//cArmor.cpp

#include "Library.h"

cArmor::cArmor(int value, int ar, int maxdurability)
{
    itsValue=value;
    itsAR=ar;
    itsMaxDurability=maxdurability;
    itsDurability=maxdurability;
}

int cArmor::getAR()
{
    return itsAR;
}

int cArmor::getValue()
{
    return itsValue;
}

int cArmor::getDur()
{
    return itsDurability;
}

void cArmor::takeDamage(int amount)
{
    itsDurability-=amount;
}

ERROR: cPlayer.cpp: In constructor `cPlayer::cPlayer(std::string, cArmor)': cPlayer.cpp:8: error: no matching function for call to `cArmor::cArmor()' cArmor.h:4: note: candidates are: cArmor::cArmor(const cArmor&) cArmor.h:8: note: cArmor::cArmor(int, int, int)

Share this post


Link to post
Share on other sites
Quote:
Original post by Kaezin
Try adding a default constructor to your cArmor class.

Finky, don't do that. It's a bad idea, for a few reasons.

Your real problem is that you're using assignment statements to initialize your member variables in your cPlayer constructor. Instead, use initialization lists.

Share this post


Link to post
Share on other sites
If you do not explicitly tell the compiler which way to construct an object in the constructors initialiser list, the compiler will attempt to use the default constructor. This is the meaning of "no matching function for call to `cArmor::cArmor()'".

The compiler lists the constructors that are available for use, the copy constructor "cArmor::cArmor(const cArmor&)", and the one you have defined "cArmor::cArmor(int, int, int)".

To tell the compiler to use the copy constructor we will use the initialiser list. We should do this for all variables. In between the name of the constructor "className()" and the opening bracket "{", place a colon to begin the initaliser list. After that, list the members you wish to initialise in the order they are declared in the class, with commas in between. Here is your example:


cPlayer::cPlayer(string name, cArmor armor)
:
itsHealth(100),
itsEnergy(100),
itsMaxHealth(100),
itsMaxEnergy(100),
itsMoney(5000),
itsName(name),
itsArmor(armor) // invoke copy ctor
{
// we no longer have to do anything here
}





This is equivalent to the following:

std::string str = "Hello";
std::string str("Hello");

That uses the appropriate constructor.
The way you were doing it was like this:

std::string str;
str = "Hello";

Which uses operator=(). This can be inefficient, for a class like std::vector which allocates memory in the constructor and may then have to reallocate when you invoke operator=(). It can be impossible, as in your case, when there is no default constructor defined.

[edit: thanks Zahlman!]

[Edited by - rip-off on May 11, 2007 3:12:39 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by Kaezin
Try adding a default constructor to your cArmor class.

Finky, don't do that. It's a bad idea, for a few reasons.

Your real problem is that you're using assignment statements to initialize your member variables in your cPlayer constructor. Instead, use initialization lists.


I stand corrected, and wish I had seen that and saved myself the embarrassment.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
After that, list the members you wish to initialise in the order they are declared in the class, with commas in between.


Correct - example is not ;) (Also, don't put a trailing comma, i.e. between the last item and the (opening curly brace for constructor body) or (semicolon terminating the constructor declaration).)

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by rip-off
After that, list the members you wish to initialise in the order they are declared in the class, with commas in between.


Correct - example is not ;) (Also, don't put a trailing comma, i.e. between the last item and the (opening curly brace for constructor body) or (semicolon terminating the constructor declaration).)


Whoops, copy and paste error from the sample code [embarrass]

Share this post


Link to post
Share on other sites
Ahhhh.. I see the light.

I didn't even know about initialiser lists but now I do!
Guys on this forum are so helpful - more helpful than my programming teacher and professors in college ever were (put together)! Maybe thats why I hated programming.

I just want to say that I love this forum. Thanks rip-off, Zahlman and Sneftel!

Share this post


Link to post
Share on other sites

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