一类叫做时钟

约翰

我需要创建一个称为Clock的类。在其中,它需要一个默认的构造函数,一个接受秒的构造函数以及一个接受小时,分钟和秒的构造函数。它需要设置h / m / s,获取h / m / s,增加/减少秒数,增加任意秒数的公共成员函数。它需要将hms转换为sec的私人成员,反之亦然。

有一个预先编写的代码可以实现我的课程。

最大的错误似乎是这样的:

'void'之前的预期unqualified-id

非常感谢你。我希望这不是太多阅读。

我无法弄清楚是什么给了我错误消息。我是C ++的新手,有关错误的研究也无济于事。

#ifndef Clock
#define Clock



class Clock{

    private:

    int hour;
    int minute;
    int second;

    int hms_to_sec() const;
    void sec_to_hms(int seconds);

    public:

    Clock();
    Clock(int seconds);
    Clock(int hours, int minutes, int seconds);


    void set_hour(int hours);
    void set_minute(int minutes);
    void set_second(int seconds);

    void add_seconds(int seconds);

    int get_hour() const;
    int get_minute() const;
    int get_second() const;

    void increment();
    void decrement();


};

#endif




#include "lab8Clock.h"



Clock::Clock(){
    hour=0;
    minute=0;
    second=0;
}

Clock::Clock(int seconds){
    if(seconds>=86400){
        hour=0;
        minute=0;
        second=0;
        return;
    }

    sec_to_hms(seconds);
}
Clock::Clock(int hours, int minutes, int seconds){


    if(hours<0 || minutes<0 || seconds<0){
        hour=00;
        minute=00;
        second=00;
        return;

    }
    if(hours>=24 || minutes>=60 || seconds>=60){
        hour=00;
        minute=00;
        second=00;
        return; 
    }
    hour=hours;
    minute=minutes;
    second=seconds;


}

void Clock::sec_to_hms(int seconds){
    int h=0, m=0, s=seconds;
    for(int i=0; i<=seconds; i++)
    {
        if(i%60==0&&i>0;){
            m++;
            s-=60;
        }
    }
    for(int j=0; j<=minutes; j++)
        if(j%60==0&&j>0){
            h++;
            m-=60;
        }
    hour=h;
    minute=m;
    second=s;

}

int Clock::hms_to_sec() const{
    int s;
    s+=(hour*3600);
    s+=(minute*60); 
    s+=second;
    return s;
}

void Clock::set_hour(int hours){
    if(hours>=24 || hours<0)
        return;

    hour=hours;
}
void Clock::set_minute(int minutes){
    if(minutes>=60 || minutes<0)
        return;

    minute=minutes;
}
void Clock::set_second(int seconds){
    if(seconds>=60 || seconds<0)
        return;

    second=seconds;
}

void Clock::add_seconds(int seconds){

    int s=seconds,m=0, h=0;
    int holder;
    if(seconds<0)
        return;


    if((second+=seconds)>86400)
        return;
    for(int k=0; k<seconds; k++){
        if(k%60==0 && k>0){
            m++;
            s-=60;

        } holder=m;
    }
    for(int k=0; k<holder; k++){
        if(k%60==0 && k>0){
            h++;
            m-=60;
        }
    }
    hour+=h;
    minute+=m;
    second+=s;
}


int Clock::get_hour() const{
    return hour;
}
int Clock::get_minute() const{
    return minute;
}
int Clock::get_second() const{
    return second;
}

void Clock::increment(){
    int i;
    i=hms_to_sec();
    if(i==86399){
        hour=0;
        minute=0;
        second=0;
        return;
    }
    add_seconds(1);
}
void Clock::decrement(){
    int i;
    if(i==0)
        return;

    second--;
}

错误示例(“秒,分钟,小时”是Clock的成员变量):

In file included from lab8exa.cpp:12:0:

lab8Clock.h:19:8: error: expected unqualified-id before ‘void’
  Clock(void);
        ^~~~

lab8Clock.h:19:8: error: expected ‘)’ before ‘void’


lab8clock.cpp:59:2: error: ‘minute’ was not declared in this scope
  minute=m;
  ^~~~~~

lab8clock.cpp:59:2: note: suggested alternative: ‘int’
  minute=m;
  ^~~~~~


lab8clock.cpp:60:2: error: ‘second’ was not declared in this scope
  second=s;
  ^~~~~~


lab8clock.cpp: At global scope:
lab8clock.cpp:64:25: error: explicit qualification in declaration of ‘int hms_to_sec()’
 int Clock::hms_to_sec() const{
                         ^~~~~

lab8clock.cpp:64:25: error: non-member function ‘int hms_to_sec()’ cannot have cv-qualifier

lab8clock.cpp:141:23:错误:“ void decrement()”声明中的显式限定void Clock :: decrement(){^

Ben

头文件中的#endif位置不正确。#endif应该在类声明后的头文件末尾。

此外,#ifndefine Clock和#define Clock会由于其类的确切名称而产生错误。将其更改为Clock__H__或类似的名称。

最后,在第46行,您有一个额外的;在if语句中,我相信51分钟应该不是分钟。经过这些更改,代码正在为我编译。

最后一件事,在将s用于

s+= ...

您需要为s设置一个值(s = 0)。为了使+ =运算符起作用,某些值必须已经在s中

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章