Receiving an Error While Defining a Class Member Function

anacy

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.

Tristan Brindle
 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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Error defining function outside the class

defining a private member function outside class definition and use of static data

Compile error when defining a member function, but only in GCC

Why defining buffer length of a buffer leads to that class's function member to loose the value of a function pointer member variable?

Defining a Single Template for a Member Function within a Class Template with Both Templates Used in Member Function

Error: Uncaught TypeError: this.beginPath is not a function while defining a prototype function

Defining unnamed class member functions?

GObject Compilation Error in Macro Expansion While Defining Class

Type conversion error in pointer to class member function

binding to boost::function class member compilation error

Error no member function declared in class when compiling

Defining "add" function in a class

Redefinition error when defining friend function inside class template

Can I ask the compiler to forbid defining a generic version of a member function of a templated class?

arguments while defining function in python

Defining template class member functions in a namespace?

Class object is initialised without defining member variables

compilation error while defining a string

Class member function throwing error on simple range function

Defining a proxy for a member function that takes a member function pointer as a template argument

Defining declared member function inside struct

Defining a static constexpr member by call to constexpr function

Error while using class in prototype of function

Error while init new class on function start

Error for defining class method as expression

Scala error when defining class

Receiving error while calling push_back() method on Vector class object

Pass member function pointer to parent class yields compiler error

sort on a class member got the error unresolved overloaded function type