1

If I have

class Something
{
public:
    Something(int whatever) : whatever_(whatever) {}
private:
    int whatever_;
}

Then what happens when I create an object on the stack

Something something;

since there is no default constructor?

1 Answer 1

1

On a conforming compiler you get a compilation error.

The following code:

class Something
{
public:
    Something(int whatever) : whatever_(whatever) {}
private:
    int whatever_;
};

Something something;

When compiled with gcc8.2 results in the following compilation error:

<source>:9:11: error: no matching function for call to 'Something::Something()' 
 Something something;
           ^~~~~~~~~    
<source>:4:5: note: candidate: 'Something::Something(int)'    
     Something(int whatever) : whatever_(whatever) {}    
     ^~~~~~~~~    
<source>:4:5: note:   candidate expects 1 argument, 0 provided    
<source>:1:7: note: candidate: 'constexpr Something::Something(const Something&)'    
 class Something    
       ^~~~~~~~~    
<source>:1:7: note:   candidate expects 1 argument, 0 provided    
<source>:1:7: note: candidate: 'constexpr Something::Something(Something&&)'    
<source>:1:7: note:   candidate expects 1 argument, 0 provided    
Compiler returned: 1

Live example available at godbolt.

Sign up to request clarification or add additional context in comments.

4 Comments

Can you add a ; after the last } and try again? It compiles for me.
Yes, a typo, fixed.
@MartinF So what compiler, compiler version and compiler options are you using?
The problem was that I didn't do a clean build after removing the default constructor. It's complaining now. Sorry...

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.