Advertisement

default constructor error

Started by June 04, 2000 04:36 PM
11 comments, last by kroiz 24 years, 7 months ago
Hellooo My problem is like that: i have a base class name sprite. from that that base class i derived a class named weapons. and a class named tank. i also have an array of pointers of the type sprite. in the tank class i made a function that create a new object of weapon and when i try to assign the weapon to the sprite pointer sprites = new weapons; i get an error: error C2514: 'weapons' : class has no constructors see declaration of 'weapons' does any body know of any bugs related to this error in the vc++ compiler 6.0 or see a way around this problem or i don't know just plz plz help. Edited by - kroiz on 6/4/00 5:51:13 PM
0 error(s), 0 warning(s)
The problem isn''t in that code you posted, it''s in the code for the weapon class. Try creating a constructor for weapons.
Advertisement
try you don''t need the () after weapon.

sprites = new weapons;
tanx for replying, but what i wrote there was just pseudo code to illustrate the problem.
weapons class have a constructor and there are no () in my real code.
plz don''t give up on me
0 error(s), 0 warning(s)
Can you post your weapons class? Or at least a little part of it? Hard to comment on why your code isn''t working without seeing the code...

One possibility... VC6 isn''t always the best with error messages, so maybe it means that there are no accessible constructors. That is, maybe your constructors are all private or protected?

---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
[edited]

Digger is correct - Visual Studio does not report an error trying to new a class with no explicit constructors.

You'll have to post your code, you must have done something REALLY weird.

#pragma DWIM // Do What I Mean!
~ Mad Keith ~

Edited by - MadKeithV on June 5, 2000 4:13:34 AM
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Advertisement
How can a compiler report an error about the fact that the class doesn''t have a constructor. If you don''t write your own constructor for a class the compiler always implements a default constructor.
i now noticed another thing :
the error says see declaration of CWeapon
and points not to the line with the definition of CWeapon but to the line with the CWeapon Declaration:

class CWeapon;

which is in the begging of the code.
is it possible that the compiler misstaken that line to be
the definition instead of just a decalration and that is why the compiler thinks i don't have a constructor.

in any case here is my CWeapon class:

class CWeapon : CSprite
{
private:
int xfactor,yfactor;
public:
CWeapon(int , int , int , int ,int , int , LPDIRECTDRAWSURFACE4 * ,int);
void Process(void);
};
CWeapon::CWeapon(
int xn,
int yn,
int cf,
int mf,
int sprm,
int spr_size,
LPDIRECTDRAWSURFACE4 *surface,
int shooter_cf)
:CSprite(xn,yn,cf,mf,sprm,spr_size,surface)
{

// compute speed and direction of sprite.
vx = CosLookUp[shooter_cf];
vy = SinLookUp[shooter_cf];

xfactor = int(4 * vx);
yfactor = int(4 * vy);

}

tanx for replying
plz help

Edited by - kroiz on June 5, 2000 5:39:28 AM
0 error(s), 0 warning(s)
Yep I think its missing your class definition, and only seeing the declaration.

Where exactly do you have that forward declaration?


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
No, I think that's just an error in the help text that gets printed out with the error.


(There is a misconception here. If you don't supply any constructors then the compiler will create a default constructor. If you supply even one, then the compiler can't create a default constructor for you, because it can't tell whether you intend to hide the default constructor or not. You see, you may wish to force the user of the class to call a certain constructor, ensuring proper inititalization of the object. If the compiler substituted a default constructor in that case, the object would not be properly constructed.)

Here is an example of why you wouldn't want the compiler to supply a default constructor:


class file
{
public:
file(string& name)
: name(name)
{
}

// these all require a filename
open() {};
close() {};
read() {};
write(){};

protected:
string name;
};



In that class declaration, you don't provide a default constructor because there is no appropriate default file name. The user must supply a file name or the file object won't work -- it will fail to open, and thus to close, read, and write. If the compiler had written a default constructor for us, then it would be possible to create file objects that could never work.

So, by providing a constructor of our own with parameters and by not providing a default constructor, we have told the compiler that it is wrong for the user to create file objects without a name. If you do have a class that will work perfectly fine with default values for all of its members, then just add in a constructor with no arguments and assign those default values.


Note that these two pieces of code are exactly the same when dealing with classes:


sprite* p = new weapon;
sprite* p = new weapon();



They both try to call the default constructor. You need to supply a default constructor, which is a constructor with no arguments. To solve the problem, just provide a default constructor for your CWeapon class, like this:


CWeapon() {};



Add in any initialization that you need, but keep in mind that you can't pass any parameters to a default constructor.

However, nothing prevents you from using your existing constructor with new, like this:


sprite* p = new weapon(5, 3, 0.5, 10, 7, 8, &surface, 57);



(substitute your own values)



- null_pointer
Sabre Multimedia


Edited by - null_pointer on June 5, 2000 8:41:49 AM

This topic is closed to new replies.

Advertisement