anacy
anacy

Reputation: 409

Receiving an Error While Defining a Class Member Function

I am doing an assignment at school, for my c++ class. The assignment is on classes. We are supposed to create a header file with just the declarations, and then create a .cpp file with the member function definitions, and then compile into object code. This is what i have at the moment, and they do something really similar in an example in the book, but i keep getting an error...

Time.h file

#ifndef TIME_H
#define TIME_H

// Class definition
class Time
{
private:
    int m_hour;
    int m_minute;
    int m_second;

public:
    Time( int hour = 0, int minute = 0, int second = 0 ); // Constructor

    // Set functions
    void set_time( int hour, int minute, int second );
    void set_hour( int hour );
    void set_minute( int minute );
    void set_second( int second );

    // Get functions
    int get_hour();
    int get_minute();
    int get_second();

    // Helper functions
    void print_universal();
    void print_standard();
    void add_second();
    void add_minute();
    void add_hour();
};

#endif

Time.cpp file

#include <iostream>
#include <iomanip>
#include "Time.h"

using namespace std;



Time::Time( int hour, int minute, int second )
{
    set_time( hour, minute, second );
}


void Time::set_time( int hour, int minute, int second )
{
    set_hour( hour );
    set_minute( minute );
    set_second( int second ); // This is where im getting the error: expected primary-expression before 'int'
}


void Time::set_hour( int hour )
{
    m_hour = ( hour < 24 && hour >= 0 ) ? hour : 0;
}


void Time::set_minute( int minute )
{
    m_minute = ( minute < 60 && minute >= 0 ) ? minute : 0;
}


void Time::set_second( int second )
{
    m_second = ( second < 60 && second >= 0 ) ? second : 0;
}

The error i'm getting, as stated above, is: " expected primary-expression before 'int' "... I don't understand it because it's a void function. Any help would be greatly appreciated.

Upvotes: 0

Views: 129

Answers (1)

Tristan Brindle
Tristan Brindle

Reputation: 16824

 set_second( int second );

You're getting the error here because of the extra int -- remove it and you'll be fine. Perhaps it was just a copy and paste error? Your calls to set_hour() and set_minute() are fine.

Upvotes: 1

Related Questions