定义类成员函数时接收错误

时间:2013-11-05 05:47:33

标签: c++ g++

我正在学校做我的c ++课程。作业是在课堂上进行的。我们应该只用声明创建一个头文件,然后使用成员函数定义创建一个.cpp文件,然后编译成目标代码。这就是我现在所拥有的,他们在本书的一个例子中做了一些非常相似的事情,但我不断收到错误......

Time.h文件

#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文件

#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;
}

如上所述,我得到的错误是:“在'int'之前预期的primary-expression”......我不理解它,因为它是一个void函数。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

 set_second( int second );

由于额外int,您在此处收到错误 - 删除它并且您会没事的。也许这只是一个复制和粘贴错误?您拨打set_hour()set_minute()的电话很好。

相关问题