Skip to main content
GameDev.net gamedev.net
🔒 Locked

error which has me stumped

Started by finky45 May 11, 2007 at 2:56 PM 6 replies 1.1k views
Original Post
finky45
finky45
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)
Kaezin
Kaezin
I'm dumb.

[Edited by - Kaezin on May 11, 2007 3:04:20 PM]
Half the people you know are below average.Trogdor the Burninator
Sneftel
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.
rip-off
rip-off
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]
Kaezin
Kaezin
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.
Half the people you know are below average.Trogdor the Burninator
Zahlman
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).)
rip-off
rip-off
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]
finky45
finky45
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!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.