2511 error with a class constructor

Started by
6 comments, last by snk_kid 19 years, 8 months ago
here the header for it: // Creature.h: interface for the Creature class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_CREATURE_H__E2EC5275_871F_45DF_A53E_344AC59FF5FC__INCLUDED_) #define AFX_CREATURE_H__E2EC5275_871F_45DF_A53E_344AC59FF5FC__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class Creature { public: int PosX; int PosY; int Size; Creature(); virtual ~Creature(); private: int Type; int Life; int Strength; int Hunger; int Vitality; int Speed; int Sight; bool Carnivore; int Food_Size_Min; int Food_Size_Max; bool Waiting; bool Hunting; bool Sleeping; bool Roaming; }; #endif // !defined(AFX_CREATURE_H__E2EC5275_871F_45DF_A53E_344AC59FF5FC__INCLUDED_) and heres the source file: #include "Creature.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// Creature::Creature(int vPosX = 0, int vPosY = 0, int vSize = 1, int vType = 1, int vLife = 100, int vStrength = 10, int vHunger = 100, int vVitality = 100, int vSpeed = 10, int vSight = 5, bool vCarnivore = true, int vFood_Size_Min = 1, int vFood_Size_Max = 3, bool vWaiting = true, bool vHunting = false, bool vRoaming = false, bool vSleeping = false) { PosX = vPosX; PosY = vPosY; Size = vSize; Type = vType; Life = vLife; Strength = vStrength; Hunger = vHunger; Vitality = vVitality; Speed = vSpeed; Sight = vSight; Carnivore = vCarnivore; Food_Size_Min = vFood_Size_Min; Food_Size_Max = vFood_Size_Min; Waiting = vWaiting; Hunting = vHunting; Sleeping = vSleeping; Roaming = vRoaming; } Creature::~Creature() { } and now the warning: --------------------Configuration: HumanEvo - Win32 Debug-------------------- Compiling... Creature.cpp c:\microsoft visual c++ 6.0 standard edition\humanevo\creature.cpp(16) : error C2511: 'Creature::Creature' : overloaded member function 'void (int,int,int,int,int,int,int,int,int,int,bool,int,int,bool,bool,bool,bool)' not found in 'Creature' c:\microsoft visual c++ 6.0 standard edition\humanevo\creature.h(13) : see declaration of 'Creature' Error executing cl.exe. HumanEvo.exe - 1 error(s), 0 warning(s) any help would be gratefull.
-----------------------------------------------The ZoloProject
Advertisement
Read that error message carefully! You are declaring a member function which has not been defined in your class definition. I only see a single constructor in your header and it doesn't have any parameters; your constructor in your cpp file on the other hand does.

cheers
sam
Your prototype in your .h does not match your parameters in .cpp

Change this line in .h:

Creature();

to:

Creature(int, int, int, int, int, int, int, int, int, int, bool, int, int, bool, bool, bool, bool);
.:<<-v0d[KA]->>:.
You didn't define the constructor like the previous posters mentioned besides from that you need to define the default constructor assuming you want to have one. Also...that's a really long paramater list it might be better to pass it as a const reference to a structure...
Thanks you guys.
-----------------------------------------------The ZoloProject
Not really; that structure would still have to be put together somehow - "The complexity has to go somewhere". I guess it could help to bundle the attributes up into a few smaller packages though.

You *could* use the named constructor idiom here (some people don't like it because it amounts to multiple-stage construction; but here it would basically just be a bunch of setter methods. Of course, some people don't like having a bunch of setter methods either ;) ). There's also the "construct from hash table" approach, though that seems weird in a lower-level language like C++. You may want to consider, since it looks like you're making some kind of RPG, that you simply don't care this much about what state a Creature is in. If combat in your game lasts long enough for creatures to get hungry, there's something rather strange ;)

"bool Carnivore" smells like a type code; you may do better off to make Carnivores a subclass of Creatures, and treat them as vegetarian by default. (Or have an abstract Creature base class.)
Similarly Waiting, Hunting, Roaming, Sleeping: my guess is that only one of these can be true at once, so this collapses into a "current_action" state, which can be replaced with a Behaviour object which is a Strategy for Creatures.

And for heaven's sake, learn about initializer lists:
Creature::Creature(int vPosX = 0, int vPosY = 0, int vSize = 1, int vType = 1, int vLife = 100,int vStrength = 10, int vHunger = 100, int vVitality = 100, int vSpeed = 10,int vSight = 5, bool vCarnivore = true, int vFood_Size_Min = 1,int vFood_Size_Max = 3, bool vWaiting = true, bool vHunting = false,bool vRoaming = false, bool vSleeping = false) :PosX(vPosX), PosY(vPosY), Size(vSize), Type(vType), Life(vLife), Strength(vStrength), Hunger(vHunger), Vitality(vVitality), Speed(vSpeed), Sight(vSight), Carnivore(bCarnivore), Food_Size_Min(vFood_Size_Min), Food_Size_Max(vFood_Size_Max), Waiting(vWaiting), Hunting(vHunting), Roaming(vRoaming), Sleeping(vSleeping){ /* Nothing more to do here. */ }

I'm realy new to C++ so I don't get much of the coding. I have come across the initializing lists thing before but I don't get the difference. it does exactly the same thing I did; Right?

And how do you guys put the code into the little scroll boxs, is there an HTML tag that does that?
-----------------------------------------------The ZoloProject
Quote:Original post by speedie
I'm realy new to C++ so I don't get much of the coding. I have come across the initializing lists thing before but I don't get the difference. it does exactly the same thing I did; Right?


There not the same even if the final result is the same, construction/initialization != assignement, all built-in types & standard library types have constructors to initialize an instance and thats what the initializer lists are for to use the constructor of data members to initializer them (also you need them to intialize constant data memebers & to call the base type constructors aswell), your code is doing assignement and not construction/initialization.

I've also got some suggestions for your "creature" type it's abit low-level and you could group some of those data members into user-defined types such as your members "PosX" & "PosY" could become an actual point type. Your just gonna end up with an epidemic symptoms of getter/setter syndrome if you leave it this way.

This member "int Type" looks like you have different types of creatures so i would take that out and make creature an abstract type and derive specialized sub-types of creature. Otherwise you'll end up with a case of type switch syndrome.

lastly this:

bool Waiting;bool Hunting;bool Sleeping;bool Roaming;


as already been mentioned looks like state information and no creature could be in all of those states at one time you could simply use an enumation type say state that changes between those four states.

with this in mind this is how i would sort of change it:

#include <cstddef>typedef size_t size_type;struct point2d {   int x, int y;   point2d(int nx = 0, int ny = 0)   : x(nx), y(ny) {}   //.... more operations here};class vitial_stats {      size_type Life;   size_type Vitality;   size_type Strength;   size_type Sight;public:   vitial_stats(size_type lif = 0,                size_type vit  = 0,                size_type str  = 0,                size_type sig  = 0)   : Life(lif), Vitality(vit), Strength(str),     Sight(sig) {}   //.... more operations here};class food {   size_type Food_Size_Min;   size_type Food_Size_Max;public:   food(size_type min = 0,        size_type max = 0)   : Food_Size_Min(min), Food_Size_Max(max) {}   //.... more operations here};class Creature {public:   enum state_type {                       WAITING,                      HUNTING,                      SLEEPING,                      ROAMING   };private:   point2d      pos;   vitial_stats stats;   food         foo;   state_type   state;   size_type    size;   int          Hunger;   int          Speed;public:   Creature(const point2d& p = point2d(),            const vitial_stats& v = vitial_stats(),            const food& f = food(),            const state_type& s = Creature::WAITING,            size_type siz = 0           )   : pos(p), stats(v), foo(f), size(siz), Hunger(0), Speed(0),     state(s) {}   virtual ~Creature() {}   //.... more operations here probably virtual & pure virtual member functions};


something like that but you get the idea.

Quote:Original post by speedie
And how do you guys put the code into the little scroll boxs, is there an HTML tag that does that?


that would be the [ source ] [ / source ] tags.

[Edited by - snk_kid on August 22, 2004 5:57:01 AM]

This topic is closed to new replies.

Advertisement