gpt4 book ai didi

C++ 明格 : Two objects of the same class are changing the data across eachother

转载 作者:行者123 更新时间:2023-11-28 03:56:05 24 4
gpt4 key购买 nike

我是新手。使用类 clsMRDateTime 并在主代码中创建 2 个对象实例。当我激活线路时:

   clsMRDateTime objMRDateTimeURL("10_05_2011");//THIS CAUSES THE PROBLEM!!!!!

它使类的第一个实例中的日期与类的第二个实例匹配。我检查了静态类变量,但无法弄清楚。

我删除了所有不活动的方法。

感谢您提供的任何帮助。迈克尔

示例测试 Main():

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "clsMRConvert.h"
#include "clsMRDebug.h"
#include "clsMRDateTime.h"



int main(int argc, char *argv[])
{
printf("Content-type: text/html\n\n");

cout << "";
cout << "Hello Test";


clsMRDateTime objMRDateTimeToday("08_25_2010");
cout << "<BR>";
string strTodayDate = objMRDateTimeToday.strGetFormatedTime("%m/%d/%Y");
cout << "objMRDateTimeToday: " << strTodayDate;
cout << "<BR>";
cout << "<BR>";

clsMRDateTime objMRDateTimeURL("10_05_2011");//THIS CAUSES THE PROBLEM!!!!!
cout << "<BR>";
//string strURLDate = objMRDateTimeURL.strGetFormatedTime("%m/%d/%Y");
//cout << "objMRDateTimeURL: " << strURLDate;
cout << "<BR>";
cout << "<BR>";


strTodayDate = objMRDateTimeToday.strGetFormatedTime("%m/%d/%Y");
cout << "objMRDateTimeToday: " << strTodayDate << " [SHOULD BE SAME AS ABOVE!!!]";

}

类 clsMRDateTime:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include "clsMRDateTime.h"


using namespace std;


clsMRDateTime::clsMRDateTime(string strDateAsString)
{

size_t found = strDateAsString.find('_');

if (found!=string::npos)
{
this->intSetDateTimeURL(strDateAsString);
}
else
{
}
}

clsMRDateTime::clsMRDateTime( time_t timetDateAsTimeT)
{
this->intSetDateTime(timetDateAsTimeT);
}




int clsMRDateTime::intSetDateTime(string strDateAsString)
{

int intDay, intMonth, intYear;
sscanf((char *)(strDateAsString.c_str()), "%d/%d/%d", &intMonth, &intDay, &intYear);

this->timetClassMainTime = this->timetMakeTime(12, 0, 0,
intMonth, intDay, intYear);

this->tmClassMainTimeTM = this->tmGetTimeInTMFormat();
}


int clsMRDateTime::intSetDateTimeURL(string strDateAsString)
{
int intDay, intMonth, intYear;
sscanf((char *)(strDateAsString.c_str()), "%d_%d_%d", &intMonth, &intDay, &intYear);

this->timetClassMainTime = this->timetMakeTime(12, 0, 0,
intMonth, intDay, intYear);

this->tmClassMainTimeTM = this->tmGetTimeInTMFormat();
}







int clsMRDateTime::intSetDateTime(time_t timetDateAsTimeT)
{
this->timetClassMainTime = timetDateAsTimeT;
this->tmClassMainTimeTM = this->tmGetTimeInTMFormat();
}


int clsMRDateTime::intSetDateTime(struct tm * tmDateAsStructTM)
{
this->timetClassMainTime = mktime(tmDateAsStructTM);
this->tmClassMainTimeTM = this->tmGetTimeInTMFormat();
}


time_t clsMRDateTime::timetMakeTime(int intHour, int intMin, int intSec,
int intMonth, int intDay, int intYear)
{

struct tm * timeinfo;
time_t time_tSeconds;
time_t rawtime;


try{

//time ( &rawtime );
timeinfo = localtime ( &this->timetClassMainTime );

timeinfo->tm_year = intYear-1900;
timeinfo->tm_mon = intMonth -1;
timeinfo->tm_mday = intDay;
timeinfo->tm_hour = intHour;
timeinfo->tm_min = intMin;
timeinfo->tm_sec = intSec;
timeinfo->tm_isdst = 0;

time_tSeconds = mktime(timeinfo);




}
catch (char * str )
{
cout << "Exception raised: " << str << '\n';

return(1);
}


return(time_tSeconds);
}



string clsMRDateTime::strGetFormatedTime(string strFormat)
{
string strFormattedTime = "";

strFormattedTime = this->strGetFormatedTimeForNewDates(strFormat, this->tmClassMainTimeTM);

return(strFormattedTime);
}


string clsMRDateTime::strGetFormatedTimeForNewDates(string strFormat, struct tm *tmNewDate)
{


string strFormattedTime;
char s[80];
size_t i;
strftime(s,80,strFormat.c_str(),tmNewDate);
strFormattedTime = s;



return(strFormattedTime);
}






time_t clsMRDateTime::timetGetGregoreanTimeStamp()
{
return(this->timetClassMainTime);
}


struct tm *clsMRDateTime::tmGetTimeInTMFormat()
{

this->tmClassMainTimeTM = localtime(&this->timetClassMainTime);

}

最佳答案

clsMRDateTime::tmClassMainTimeTM 是指向 struct tm指针,所以当您执行 this->tmClassMainTimeTM = localtime( &this->timetClassMainTime); 你实际上是在保存一个指向 localtime 返回值的指针。

localtime 函数重用了一个静态的tm 结构,所以返回值每次都是相同的。您所做的是设置它,以便 clsMRDateTime 的每个实例最终都存储指向相同 struct tm 的指针,因此它们最终都代表最近设置的日期/时间。

你需要做的是让 tmClassMainTimeTM 不是一个指针(让它只是一个 struct tm)然后做一些像 this->tmClassMainTimeTM = *本地时间(&this->timetClassMainTime);.

更好的方法是查看 localtime_r 的手册页,它是线程安全的,允许您传入自己的结构并使用它。

关于C++ 明格 : Two objects of the same class are changing the data across eachother,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3565837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com