【发布时间】: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