Before I start, I'd just like to point out I've Googled this question for a while now and haven't found a satisfactory answer. I'm also aware of a very similar question on Stack Overflow, but it doesn't help me entirely with my situation.
Here's my code:
class A
{
private:
int x;
int y;
public:
A( int x, int y );
// other member functions
};
class B
{
private:
int score;
A* a;
public:
B( int x, int y ); // Regular constructor
// other member functions
};
Implementation:
// Default constructor with member initialisation
A::A( int x, int y )
: x( x ), y( y )
{}
// Default constructor with member initialisation
B::B( int x, int y )
: score( 0 ), a( x, y )
{}
The problem I have is with initialising the pointer member variable *a in class B. We are required to pass the x, y coordinates to the class B constructor, but I cannot get g++ to compile this as it is expecting a pointer instead of two integers. I'm having trouble wrapping my head around how to call an initialisation to the *a pointer with x, y as my parameters.
In the assignment question, we aren't asked to initiate the *a pointer member variable to a null pointer, nor are we given a pointer with which to initialise it. We are only given x & y coordinates.
The error I receive when compiling with g++ is:
error: expression list treated as compound expression in mem-initializer [-fpermissive]
: score( 0 ), a( x, y )
I'd appreciate any feedback, thanks in advance for your help!
A* a;member is very bad style, it is something you might be forced to do in school but you would never do that in a real world program.