【问题标题】:C++ Constructor with other class-data-type具有其他类数据类型的 C++ 构造函数
【发布时间】:2013-12-01 17:01:48
【问题描述】:

首先,大家好,

我是这个论坛的新手,所以我希望我没有犯任何新手错误,如果是这样 - 请纠正我:)。第二:英语不是我的母语,所以我希望我能把我的问题说清楚。

关于我的问题:

我必须编写一个包含 2 个类的 C++ 程序,1 类 Date 用 3x unsigned int 表示日、月和年,第 2 类 Time 用 2x unsigned int 表示分钟和小时,以及 1x Date。

  • 每个类都有一个调用重载构造函数的默认构造函数(我们必须使用初始化列表) 我的问题是,我不知道如何在 Time 类中使用 Date 类的成员。

我知道我的问题不是很清楚,所以这里是我的代码的简化版本: 主要:

#include "Date.hpp"
#include "Time.hpp" //(+ iomanip / iostream)

int main(){
Date d1; //Calls Default Constructor of class Date, which initialises with (1,1,2000)
Time t1(d1,23,30); //should call ovrld-constr. of class Time and keep the date of d1

在我的“Time.cpp”中,我尝试了类似:

Time::Time(Date x, unsigned int y, unsigned int z)
  : d(Date::Date(x)), minute(y), hour(z) //try to call the def constr of class Date
{
return;
} 

Time::Time() : Time(Date::Date(), 00, 00) //should call the ovrld constr of class Time
{
return;
}

但它当然不起作用......编译器总是说:

main.cpp:(.text+0x35c):undefined reference to `Time::Time()'

如果有帮助,这里是我的(完全可用的)“Date.cpp”的摘录:

Date::Date(unsigned int x, unsigned int y, unsigned int z)
 : day(x), month(y), year(z)
{
if(valiDate(day, month, year)==false){ //ValiDate is a function which checks the input                          
day=1;month=1;year=2000;} //default date, if Date is not Valid
return;
} 

Date::Date() : Date(1,1,2000)
{
return;
}

我希望这足以说明我的问题,如果没有,我可以随时向您展示完整的源代码。重要提示:我不允许(!)编辑 main.cpp。 另外,如果有人可以解释我的错误,而不仅仅是发布一个完整的解决方案,那将不胜感激,因为我认为我 - 在最坏的情况下 - 甚至不理解的解决方案对我的学习没有多大帮助.

提前感谢您的帮助,

最好的问候。

编辑:

对不起,我忘记了两个 class.hpp。

1)工作日期.hpp(摘录)

class Date
{

public:
  Date();
  Date(unsigned int, unsigned int, unsigned int);


private:

  unsigned int day, month, year; 
  bool valiDate(unsigned int, unsigned int, unsigned int);
};

2)我的(不工作)Time.hpp。

class Time
{

  public:

  Time();
  Time(Date, unsigned int, unsigned int);

  private:

  unsigned int minute, hour;
  Date d;

};

【问题讨论】:

  • 您是否已将 Time.cpp 添加到您的项目中?
  • 提供一个short, self-contained, correct example,而不是一堆乱七八糟的sn-ps代码!
  • 向我们展示您的 Time.hpp
  • 导致main.cpp:(.text+0x35c):undefined reference to `Time::Time()'的编译命令是什么?
  • 就目前而言,您有很多编译错误,其中没有一个是对`Time::Time()'的未定义引用。如前所述,提供一个short, self-contained, correct example 而不是一堆乱七八糟的代码sn-ps。

标签: c++ class c++11 constructor


【解决方案1】:

我复制了你的项目,发现了几十个编译和链接错误,但没有你指定的错误。这是我找到的:

class Time按值包含一个Date类型的数据成员,所以你需要在Time.hpp中#include Date.hpp。如果您还没有这样做,您需要将include guards 添加到您的标题中。

您不需要在 Time 的成员初始化程序中显式指定 Date 构造函数,因为无论如何都会调用它:

Time::Time(Date x, unsigned int y, unsigned int z)
  : d(Date::Date(x)), minute(y), hour(z) //try to call the def constr of class Date
{
    return;
} 

可以是:

Time::Time(Date x, unsigned int y, unsigned int z)
    : d(x), minute(y), hour(z)
{

} 

请注意,您也不需要在构造函数中编写return 语句。 另请注意,: d(x) 调用 Date 的复制构造函数,而不是默认构造函数。您还没有定义复制构造函数,因此正在为您隐式创建一个复制构造函数来进行浅拷贝。

您还从另一个构造函数调用了一个构造函数:

Time::Time() : Time(Date::Date(), 00, 00)
Date::Date() : Date(1,1,2000)

C++11 支持委托构造函数,但并非所有编译器都支持,但您可以自己初始化成员:

Time::Time() : minute(0),hour(0)
Date::Date() : day(1),month(1),year(2000)

【讨论】:

  • 是的,我知道“构造函数调用构造函数”是 c++11,但我们必须尽可能使用它;)其他提示,例如:在 Time.hpp 中包含 Date.hpp 和我不需要专门调用 Date::Date() 非常有帮助,因为我只需要将“{}”添加到“Time::Time() : Time(Date::Date(), 00, 00)” ,非常感谢你 :) 我能以某种方式“投票”你的答案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-17
  • 2018-06-10
  • 2015-08-28
  • 1970-01-01
  • 2018-04-10
  • 2010-09-07
相关资源
最近更新 更多