the class constructor

Started by
8 comments, last by crazedfool 20 years ago
thsi piece of code says i have no default constructor for class Stock:

#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

char choose;

class Stock
{
	
	public:

		Stock( string *, char, int, int );

	private:
		
		string name;
		char type;
		int indexnumber;
		int value;
		
};

Stock::Stock( string *namePtr, char typeS, int indexnumberS, int valueS)
{ 

	name = *namePtr;
	type = typeS;
	indexnumber = indexnumberS;
	value = valueS;

}
I thought I made the constructor. the error i get is:

c:\documents and settings\administrator\desktop\c++\string test.cpp(49) : error C2512: 'Stock' : no appropriate default constructor available
[edited by - 3dmodelerguy on March 21, 2004 4:19:48 PM]
Advertisement
You made a constructor, you didn''t make a default constructor, which is a constructor that takes no arguments.
Exactly right... if you want to allocate an object statically, youll need to create a default constructor for it.
Alternatively, you can store a pointer and create it dynamically using new
is there any way to make that the default constructor or should i just make a class method with the same parameters?

[edited by - 3dmodelerguy on March 21, 2004 4:25:57 PM]
why does he have to make a default constructor also? i have classes in my game that have contructors (which take parameters) but no defualt constructor. and im not getting any errors/problems. so why does he have to?

by default contructor, you mean

Stock(){}
~Stock(){} //this is default destructor


correct? if so, i just answered the above persons question
FTA, my 2D futuristic action MMORPG
Stock::Stock(){    name = null;    type = ''\0'';    indexnumber = 0;    value = 0;}


That''s what the default constructor for your Stock class may look like.

What it''s basically doing is setting all the member variables to default values so the class will work.

Block myBlock();//orBlock* myBlock = new Block(); 
new post made

[edited by - 3dmodelerguy on March 21, 2004 5:10:11 PM]
quote:Original post by graveyard filla
why does he have to make a default constructor also? i have classes in my game that have contructors (which take parameters) but no defualt constructor. and im not getting any errors/problems. so why does he have to?

by default contructor, you mean

Stock(){}
~Stock(){} //this is default destructor


correct? if so, i just answered the above persons question


Well your default constructor is empty - it doesnt have to be.However, in his case it doesnt exist at all. Look at henrym''s default constructor and youll get an idea.

Also, the destructor never takes parameters as it is never called explicitly - its called either when the object goes out of scope or is deallocated with delete.
i still dont understand why he needed a default contructor on top of his regular contructor. this is what one of my classes looks like : could you please tell me why i dont need a default contructor? (im getting no errors/problems)

class Entity		//Entity represent all the "Entity's" in the game, the gloves and player{	public:	//constructor initializes starting direction and mode			Entity(int start_direction, int start_mode,int ID)	{		direction = start_direction; 		mode = start_mode;		death_spin = 0; //init death_spin to 0 (had to happen somewhere, huh?)		int update_counter = 0;		changing_states = false;		ammo = 0;		this->ID = ID;	}		~Entity(){}.... more stuff here



[edited by - graveyard filla on March 21, 2004 5:08:18 PM]
FTA, my 2D futuristic action MMORPG
AFAIK it is not necessary to have a default constructor. When you allocate an instance of that class are you passsing the right parameters?

As in:
Stock temp; //would give you that error
Stock temp("hello", ''g'', 5, 10); //should not

This topic is closed to new replies.

Advertisement