How I can do it?
I want a constructor of a object to have parameters, this object is inside other class..the compiler say the class dont have proper default constructors..(error C2512)

...
class Game{

private:
	Player P1;
	Player P2;


public:
	 Game(void);//constructor
...
class Player{
private:
	int x;//player x/y position
	int y;


public:
	 Player(int _x, int _y);//constructor
	~Player(void);//destructor


};
...
Player::Player(int _x, int _y){

	x = _x;
	y = _y;

}

**EDIT**
Also, if I do that:

class Game{

private:
	Player P1(10, 210);
	Player P2(310, 210);
...

I get "C2059: syntax error : 'constant' "

Recommended Answers

All 5 Replies

#include "stdafx.h"

class Player{
private:
	int x;//player x/y position
	int y;


public:
	 Player(int _x, int _y);//constructor
	~Player(void);//destructor


};


Player::Player(int _x, int _y){

	x = _x;
	y = _y;

}
int main(int argc, char* argv[])
{
	printf("Hello World!\n");

	Player p1(10,20);
	Player p2(310,210);
	
	return 0;
}

I just try this sample program and it compiles without a problem.
and I just google your error. Probally you are not the only one who
get a error like this. well first search is MSDN page that contain details about this error.

it says torken error.

so sounds like it's little bit complicated to see this without looking at other source codes and dependencies.

anyway a one pice of advice , to avoid unnecessary problems always use a standard. Don't use _ starting variables. They are reserved to use internally inside the C++ compiler itself. Try it again with removing _ starting variable names.

If you need to learn about how to name your variables properly google "coding standards C++"

* you can use _ characters on variables but don't start with it.

But my Player object is inside other class ( I though that was the problem) I also using .h and .cpp files to each class...
Im attaching my files to .rar file..
It was compiling OK until I put the params on the Player constructor..
Thanks for the tips

and for your first question (C2512) , just google that error and
you will find the answer. I can't post duplicate here. It's violation of member rules.

the problem is think that , if you call

Player p;

inside main , it will looking for the default constructor and calls it.
when you overload it there is no default constructor ( there is a reason for this rule think why ? ).

It was compiling OK until I put the params on the Player constructor..

If you overload the default constructor then the compiler no longer put a default constructor ( this is not just a rule there is a reason for this think why ? )
So you can't say

Player p ;

IDEA:
if you want to write

Player p ;

like that you should define the default values for your overloaded constructor .Like this

class Player{
private:
    int x;//player x/y position
    int y;


public:
     Player(int _x=0, int _y=0);//constructor
    ~Player(void);//destructor


};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.