我在 3 个文件中有 3 个类。
我希望能够创建一个新的 Recordatorio 类,其中包含 Fecha 和 Horario 类。
这是我在 main 中的功能:
Recordatorio recordatorio(Fecha(5, 10), Horario(9, 0), "Clase Algo2");
注意:Fecha(5,10) 和 Horario(9,10) 工作得很好。
这是我在 Recordatorio.cpp 中的代码:
#include "Recordatorio.h"
#include "Fecha.h"
#include "Horario.h"
#include <iostream>
#include <string>
using namespace std;
Recordatorio::Recordatorio(Fecha fecha, Horario horario, string mensaje){
fecha_ = fecha;
horario_ = horario;
mensaje_ = mensaje;
}
void Recordatorio::mensaje(){
cout << mensaje_ << endl;
}
这在我的 Recordatorio.h 中:
#ifndef RECORDATORIO_H
#define RECORDATORIO_H
#include "Fecha.h"
#include "Horario.h"
#include <string>
class Recordatorio
{
public:
Recordatorio(Fecha fecha, Horario horario, std::string mensaje);
void mensaje();
private:
Fecha fecha_;
Horario horario_;
std::string mensaje_;
};
#endif // RECORDATORIO_H
我在 Recordatorio.cpp 中遇到的错误如下:
错误:没有用于调用“Fecha::Fecha()”的匹配函数
编辑:Close.h
#ifndef FECHA_H
#define FECHA_H
using uint = unsigned int;
class Fecha{
public:
Fecha(uint mes,uint dia);
uint mes();
uint dia();
void incrementar_dia();
private:
uint mes_;
uint dia_;
};
#endif // FECHA_H
日期.cpp
#include "Fecha.h"
uint dias_en_mes(uint mes) {
uint dias[] = {
// ene, feb, mar, abr, may, jun
31, 28, 31, 30, 31, 30,
// jul, ago, sep, oct, nov, dic
31, 31, 30, 31, 30, 31
};
return dias[mes - 1];
}
uint Fecha::mes() {
return mes_;
}
uint Fecha::dia() {
return dia_;
}
Fecha::Fecha(uint dia, uint mes){
dia_ = dia;
mes_ = mes;
}
void Fecha::incrementar_dia(){
uint maximo_dia_mes = dias_en_mes(mes_);
if (dia_ < maximo_dia_mes){
dia_++;
} else {
dia_ = 1;
if (mes_ != 12){
mes_++;
} else {
mes_ = 1;
}
}
return;
}
小时.h:
#ifndef HORARIO_H
#define HORARIO_H
using uint = unsigned int;
class Horario
{
public:
Horario(uint hora, uint min);
uint hora();
uint min();
private:
uint hora_;
uint min_;
};
#endif // HORARIO_H
时间表.cpp:
#include "Horario.h"
using uint = unsigned int;
Horario::Horario(uint hora, uint min){
hora_ = hora;
min_ = min;
}
uint Horario::hora() {
return hora_;
}
uint Horario::min() {
return min_;
}
Recordatorio::Recordatorio(Fecha fecha, Horario horario, string mensaje) :
fecha_(fecha),
horario_(horario),
mensaje_(mensaje) {
}
这现在将为您的类使用默认的复制构造函数,而不是构造一个空对象然后分配它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句