Create object inside class

I am trying to create an object of class Car inside class Auto.Car has a constructor that receives an int parameter.When I declare the object, I am not able to send this parameter to Car constructor, I get errorC2059:sintax error 'constant'.Here is the code:
class Car
{
private:
int sides;

public:
Car(int nsides)
{
sides=nsides;
}

};

class Auto
{
private:
Car test(5);

public:
Auto()
{
}
};

Does anyone has an idea how to send a parameter to constructor?

Thanks
Car test(5); <-- calling constructors like this is not valid in class definitions. To call an object's ctor you can use an initializer list:

1
2
3
4
5
6
7
8
9
10
11
class Auto
{
private:
  Car testa;
  Cat testb;

public:
  Auto() : testa(5), testb(6)
  {
  }
};
Thanks, it worked for this simple example, but I am having a hard time on a more complex program. when I called the ctor with initializer list and built it, I had a compiler error saying that default constructor for Car class is missing. I added it(Car(){}) and now the objects testa and testb are using default constructor.
I found the error!!
Thanks, you really helped me!!
Topic archived. No new replies allowed.