c++ - 具有另一个类对象属性的类的构造函数

标签 c++ object constructor attributes declaration

构造函数声明:

Funcion(std::string cveFunc=" ", int numP = 0, Hora hour(0,0), int room=0);

构造函数:

Funcion::Funcion(std::string cveFunc, int numP, Hora hour(), int room) : 
    cveFuncion{cveFunc}, numPeli{numP}, hora hour = {}, sala{room}{}

问题出在 hour 属性上,我不知道如何正确声明它。

这是类 hora 的构造函数:

内部类:

Hora(int hhh=0, int mmm=0);

课外:

Hora::Hora(int hhh, int mmm) : hh{hhh}, mm{mmm} {}

最佳答案

使用

Funcion::Funcion(std::string cveFunc, int numP, Hora hour(), int room)

是错误的,因为在该上下文中 hour 被声明为不带参数并返回 Hour 的函数。您需要删除 ()。使用:

Funcion::Funcion(std::string cveFunc, int numP, Hora hour, int room)

假设hora是类的成员变量,成员初始化也需要更新为:

cveFuncion{cveFunc}, numPeli{numP}, hora {hour}, sala{room}

放在一起,你有:

Funcion::Funcion(std::string cveFunc, int numP, Hora hour, int room) :
        cveFuncion{cveFunc}, numPeli{numP}, hora {hour}, sala{room}{}

关于c++ - 具有另一个类对象属性的类的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55155927/

相关文章:

c++ - std::lock_guard 可以中断吗?

javascript - Typescript - 如何从变量对象访问变量对象属性

c++ - 为什么当我创建一个包含 N 个对象的 vector 时,CTOR 没有被调用 N 次?

python - 自动保存构造函数参数

c++ - 排序自定义容器

c++ - 在 C++ 中,您需要在两个方向上重载 operator== 吗?

c++ - VC++ Debug模式:批量编辑 std::vector<int> 值?

java - 每次程序运行时创建一个新的但不同的对象

javascript - 如何从表示更改的对象数组中获取最终状态

java - 什么时候在java中使用Long vs long?