C++,在另一个对象构造函数中将对象作为参数传递

加斯帕阿德拉尼亚

我在 3 个文件中有 3 个类。

  • 日期.h
  • 小时.h
  • 提醒.h

我希望能够创建一个新的 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C ++:如何将一个对象传递给另一个对象构造函数?

传递c ++对象作为指针,以在Rcpp中的另一个函数中重用

使用另一个QML对象作为参数从C ++调用QML函数

c#是否可以在创建静态对象的同时将其传递给另一个对象的构造函数或方法

C++ 如何根据作为输入传递的参数调用一个构造函数或另一个构造函数?

直接在 C 中将函数的返回(和调用)作为另一个参数直接传递

在cython中将c ++对象传递给另一个对象的方法的方法

构造函数中的 C++ 语法错误 - 参数是对来自另一个类的对象的引用

C ++中带有另一个模板类作为参数的构造函数

将任何类型的对象的函数传递给C ++中的另一个对象

C ++:将一个对象复制到构造函数中的另一个对象

如何在WPF中将对象从一个窗口绑定到另一个窗口中的另一个控件?(C#)

在构造函数C ++中调用另一个对象的方法

将指向一个C函数的指针作为另一个函数的参数传递

对象的方法作为c ++中另一个类的方法参数

如何在C#中将对象列表中的值分配给另一个列表

是否可以将C ++指针分配给Python对象的属性,然后在另一个C ++函数中将其作为C ++指针检索?

如何在C ++中将类对象的引用正确传递到另一个类对象

将对象作为参数传递给C ++中的函数

C ++将具有重载构造函数的对象添加到另一个对象

C ++将一个对象传递给另一个对象?

将一个对象的地址传递给另一个对象 C++

在C中将值从一个函数传递给另一个函数

Arduino C ++将对象作为参数传递

C++:将对象作为参数传递

C ++实例化对象,该对象在另一个没有指针的类构造函数中没有默认构造函数

如何在构造函数中将对象数组作为参数传递,然后将传递的数组复制到另一个数组(仍在构造函数中)

C ++-如何在另一个类中使用私有构造函数实例化对象

如何在 C++ 中使用具有另一个类对象的构造函数?