Sign in to follow this  

Default Arguments in Class Constructors

This topic is 3576 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I can't seem to get the to work:
#include <iostream>

using namespace std;

class Skater
{
      public:
             int m_speed;
             
             Skater(int speed = 50);
             void DisplaySpeed();
};

void Skater::DisplaySpeed()
{
     cout << "Skater speed is: " << m_speed << endl;
}

Skater::Skater(int speed)
{
    m_speed = speed;                   
}

int main()
{
    Skater skate();
    Skater skate2(75);
    
    skate.DisplaySpeed();
    skate2.DisplaySpeed();
    
    system("pause");
    return 0;
}

skate2 works fine when I comment out "skate.DisplaySpeed();", here's the error I get: 29 C:\Dev-Cpp\Class.cpp request for member `DisplaySpeed' in `skate', which is of non-class type `Skater ()()' How is this different from regular default function arguments?

Share this post


Link to post
Share on other sites
Your actual problem is that

Skater skate();

doesn't actually define a default constructed Skater object. Instead it declares a function named skate that takes no parameters and returns a Skater. Change it to:

Skater skate;

Share this post


Link to post
Share on other sites

This topic is 3576 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this