gpt4 book ai didi

c++ - 无法调用构造函数

转载 作者:行者123 更新时间:2023-11-28 01:28:25 26 4
gpt4 key购买 nike

我必须制作一个显示小时和分钟的计时器,但出现此编译错误。

In member function ‘std::string Timer::toString() const’: timer.h:43:17: error: cannot call constructor ‘Timer::Timer’ directly [-fpermissive]

我不明白为什么会这样,也不知道这意味着什么。请帮忙。我认为它与 toString 函数有关,但我不确定

#ifndef TIMER_H_
#define TIMER_H_

#include <string>
#include <sstream>



class Timer {
private:
int horas;
int minutos;

public:
Timer();
Timer(int, int);
Timer(const Timer&);

int getHoras() const;
int getMinutos() const;

void operator =(const Timer&);
void operator +=(const Timer&);
std::string toString() const;



};

std::string Timer::toString() const {
std::stringstream aux;

Timer :: Timer() : horas(0), minutos(0) {}

Timer :: Timer(int hh, int mm) {
if(mm > 60){
horas = hh + (mm/60);
}else {
horas = hh;
minutos = mm;
}
}

Timer :: Timer(const Timer &source) : horas(source.horas), minutos(source.minutos) {}

int Timer :: getHoras() const {
return horas;
}

int Timer :: getMinutos() const {
return minutos;
}

void Timer :: operator =(const Timer &source) {
horas = source.horas;
minutos = source.minutos;
}

void Timer :: operator +=(const Timer &source) {
if((source.minutos+minutos) > 60){
horas = horas + 1;
minutos = (minutos + source.minutos) % 60;
}else {
minutos = minutos + source.minutos;
}
horas = (horas + source.horas)%24;
}

bool operator ==(const Timer &izq, const Timer &der) {
return ( (izq.getHoras() == der.getHoras())&&(izq.getMinutos() == der.getMinutos() );
}

bool operator >(const Timer &izq, const Timer &der) {

if(izq.getHoras() > der.getHoras()){
return true;
}else if(izq.getHoras() == der.getHoras()){
if(izq.getMinutos() > der.getMinutos()){
return true;
}
}

return false;
}
#endif

最佳答案

您在函数定义中缺少 }。此外,如果函数的类型声明为 string。然后您必须返回一个字符串

std::string Timer::toString() const {
std::stringstream aux;
std::string youMustReturnAString;
return youMustReturnAString;
}

您还缺少第 77 行末尾的 ):

bool operator ==(const Timer &izq, const Timer &der) {
return ((izq.getHoras() == der.getHoras()) && (izq.getMinutos() == der.getMinutos()));
}

关于c++ - 无法调用构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52769832/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com