在向量中推送结构会覆盖时间分量
Pushing struct in vector overwrites time component
我正在编写一个简单的 CLI 日历,我将计划活动的数据存储在一个简单的结构中,该结构由一些整数、一个字符串和一个指向 tm
结构的指针组成。
但是,当我将这些结构推送到一个向量中时,所有 *tm
,并且只有那些,会被我推送的最新结构之一覆盖。其他一切都保持不变。
因此,如果我为 10/02/2016 推送 "A" 并为 5/11/2016 推送 "B",我将按计划在 2016 年 5 月 11 日看到这两个条目,尽管所有其他细节都保持不变。
谁能帮我找出问题所在?
如果您想查看代码,请在此处
while (getline(caleList, tempstring)) {
cout << tempstring << endl;
Appointment tempapp = unwrapAppointment(tempstring);
apps.push_back(tempapp);
}
这里我正在从保存文件中读取,通过 unwrapAppointment
解码条目(我检查过它正在工作),然后简单地将它们推回。
编辑:代码的其他部分:
struct Appointment
{
int ID;
bool alarm;
tm* dateAndTime;
string text;
};
这是造成问题的结构
尝试读取活动时的日志:
Calendar Module Activated. 1 to mark an activity, 2 to read all activities, anything else to return to main.
> 2
Encoded activities:
0|61397616601|First|1
0 First Fri Aug 13 14:30:01 3915 1
1|61424667901|Second|0
1 Second Wed Jun 21 16:45:01 3916 0
2|61416011701|Third|1
2 Third Mon Mar 13 12:15:01 3916 1
Decoded activities:
Activity number 0: First, set for the day 13/2/2016 at 12:15
Activity number 1: Second, set for the day 13/2/2016 at 12:15
Activity number 2: Third, set for the day 13/2/2016 at 12:15
编码的活动使用 mktime 和 localtime 将 tm 转换为 time_t,反之亦然。另外,我刚刚注意到我搞砸了月份和年份的转换,可能是问题所在吗?
我猜你的 tm
指针是使用 gmtime
或 localtime
设置的。您是否意识到那些函数总是 return 相同的指针?如果您存储该指针,其内容将被下一次调用 gmtime 或 localtime 覆盖?
推测性地写一个没有代码的答案,因为我很确定这是问题所在:如果您将 tm *timestamp
存储为 return 由 return struct tm*
在您的对象中,然后在调试器中简单查看您的数据将显示所有这些时间戳将具有相同的指针值 - 指向函数内部的单个 static struct tm
你打电话因此,每当您再次调用该函数时,所有值都将更改为新值。
将其更改为 tm timestamp;
然后执行 timestamp = *(whatever());
而不是 timestamp = whatever();
[技术上不需要额外的括号]
我正在编写一个简单的 CLI 日历,我将计划活动的数据存储在一个简单的结构中,该结构由一些整数、一个字符串和一个指向 tm
结构的指针组成。
但是,当我将这些结构推送到一个向量中时,所有 *tm
,并且只有那些,会被我推送的最新结构之一覆盖。其他一切都保持不变。
因此,如果我为 10/02/2016 推送 "A" 并为 5/11/2016 推送 "B",我将按计划在 2016 年 5 月 11 日看到这两个条目,尽管所有其他细节都保持不变。
谁能帮我找出问题所在?
如果您想查看代码,请在此处
while (getline(caleList, tempstring)) {
cout << tempstring << endl;
Appointment tempapp = unwrapAppointment(tempstring);
apps.push_back(tempapp);
}
这里我正在从保存文件中读取,通过 unwrapAppointment
解码条目(我检查过它正在工作),然后简单地将它们推回。
编辑:代码的其他部分:
struct Appointment
{
int ID;
bool alarm;
tm* dateAndTime;
string text;
};
这是造成问题的结构
尝试读取活动时的日志:
Calendar Module Activated. 1 to mark an activity, 2 to read all activities, anything else to return to main.
> 2
Encoded activities:
0|61397616601|First|1
0 First Fri Aug 13 14:30:01 3915 1
1|61424667901|Second|0
1 Second Wed Jun 21 16:45:01 3916 0
2|61416011701|Third|1
2 Third Mon Mar 13 12:15:01 3916 1
Decoded activities:
Activity number 0: First, set for the day 13/2/2016 at 12:15
Activity number 1: Second, set for the day 13/2/2016 at 12:15
Activity number 2: Third, set for the day 13/2/2016 at 12:15
编码的活动使用 mktime 和 localtime 将 tm 转换为 time_t,反之亦然。另外,我刚刚注意到我搞砸了月份和年份的转换,可能是问题所在吗?
我猜你的 tm
指针是使用 gmtime
或 localtime
设置的。您是否意识到那些函数总是 return 相同的指针?如果您存储该指针,其内容将被下一次调用 gmtime 或 localtime 覆盖?
推测性地写一个没有代码的答案,因为我很确定这是问题所在:如果您将 tm *timestamp
存储为 return 由 return struct tm*
在您的对象中,然后在调试器中简单查看您的数据将显示所有这些时间戳将具有相同的指针值 - 指向函数内部的单个 static struct tm
你打电话因此,每当您再次调用该函数时,所有值都将更改为新值。
将其更改为 tm timestamp;
然后执行 timestamp = *(whatever());
而不是 timestamp = whatever();
[技术上不需要额外的括号]