Domanda

I created a class with a constructor that takes an int to determine the size of a linked list the object has. The problem I'm having is I need to be able to call this constructor when this object is instantiated as a private member of another class. So basically:

class A {
public:
    A();
    A(int size);
};

class B {
    const int size = // any number > 0
private:
    A a(size);
};

I get this error:

constant "B::size" is not a type name

I've tried searching online, but I can't come across this specific issue. It could be that I'm struggling to word the question correctly... it's a weird issue I haven't seen yet. Any help is appreciated!

È stato utile?

Soluzione

You cannot call the constructor with parameters in the member variable declaration.

You can implement a constructor for B and do it there.

B::B() : a(size) {}

Altri suggerimenti

You have to do in the B constructor, using an initializer list:

class B
{
public:
    B() : a(size)
    {}

private:
    A a;
    const int size = ...;
};
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top