C ++ Mingw:同一个类的两个对象正在相互更改数据

时间:2010-08-25 12:37:39

标签: c++ class mingw

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

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

它使类的第一个实例中的日期与类的第二个实例匹配。我检查了静态类变量,但无法解决这个问题。

我删除了所有非活动方法。

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

Sample Test 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);

    }

3 个答案:

答案 0 :(得分:1)

您没有显示clsMRDateTime.h,但我在该文件中打赌您将成员变量声明为static。他们不应该。

答案 1 :(得分:1)

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

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

您需要做的是使struct tm不是指针(只使其成为tmClassMainTimeTM),然后执行类似struct tm的操作。

最好查看this->tmClassMainTimeTM = *localtime(&this->timetClassMainTime);的手册页,它是线程安全的,允许您传入自己的结构并使用它。

答案 2 :(得分:0)

我会仔细检查对gmtime()或localtime()的任何调用,因为它们共享并重用静态分配的结构。

唯一突出的其他候选者是第一个构造函数中的空else语句。