0

I have a class called sample.

class sample
{
private:
    int Age;
public:
    sample() : Age() {}
    sample(int age)
    {
        this->Age = age;
    }
};

In main

int main()
{
    sample* s = new sample();
    s(21);

    return 0;
}

I want to pass values to the parameterized constructor through pointer object but it's giving this error

expression preceding parentheses of apparent call must have (pointer-to-) function type

1 Answer 1

1

You've already constructed the object when did new sample(), so you can pass your constructor values right there. You cannot construct the object twice.

Do it like this:

sample* s = new sample(21);

or

sample* s;
s = new sample(21);
Sign up to request clarification or add additional context in comments.

2 Comments

Or even: sample s { 21 }; There's no obvious need for a pointer here at all.
Its possible he requires a pointer for some subsequent use. Also, did you mean s = sample{21}?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.