Jump to content
  • Advertisement
Sign in to follow this  
Kenny77

Default Arguments in Class Constructors

This topic is 3758 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!