0

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!

5
  • 2
    note: the 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. Commented Mar 6, 2016 at 20:42
  • Thanks for the feedback. What is the value in teaching this kind of class implementation then, if it's considered bad practice? Commented Mar 6, 2016 at 20:42
  • Being optimistic... maybe so you can learn why it is a bad idea. Commented Mar 6, 2016 at 20:44
  • 1
    You have commented in your code that the two constructors are 'default'. IMHO, they are not. (But probably not important for this post.) Be aware that the compiler, in most simple instances, does provide for you a default ctor, unless you tell it not too, or actually provide one. Commented Mar 6, 2016 at 20:51
  • Thanks Douglas, appreciate the feedback. We have been asked to explicitly define our own constructor, as we are using a pointer member variable, so I can only assume the purpose of the exercise is to demonstrate the difference between the compiler-issued ctor and our own ctor with pointer member variable initialisation. Commented Mar 6, 2016 at 20:53

3 Answers 3

0

The way you have stated your question I have assumed that B is the creator and owner of A* a. With that requirement it is very odd to use a pointer. The natural and obvious solution to this is to use a regular member.

Now the syntax problem you are having can be solved with

B::B( int x, int y )
:   score( 0 ), a( new A(x, y) )
{}

This is a bad idea, because now you have to clean up a in your destructor.

B::~B(){
    delete a;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This makes sense, as we are expected to implement our own destructor to clear the pointer member variable. Thanks for your help!
0

Try

B::B(int x, int y)
    : score(0), a(new A(x, y))
{}

Your member a is of type A*, A* variable cannot be instanciated by 2 ints. You can create an instance of A in heap and take a pointer to it by new operator. With this pointer you initialize your a member. Ofc, you should also consider to free memory by delete operator in your destructor.

Comments

0

There are 2 problems: First: A* a; only creates a pointer of type class A, but its not pointing to any instance of the class. It requires: A* a = new A;

Second: you can not initialize a class pointer with int

Instead you may pass an instance of class along with passing x,y to its constructor:

B::B( int x, int y )
    :   score( 0 ), a(new A(x,y))
{

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.