Need help with Struct tm passing!
Dec 3, 2014 at 1:04pm UTC
Hi there,
I'm trying to pass a Struct tm as a variable in a function to another class, to get it to print the time.
This is my current code for each of the functions:
1 2 3 4 5 6 7 8 9 10 11
void Game::Run()
{
initialise();
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
//cout << "Updated at: " << asctime(timeinfo) << endl;
components[0]->Update(timeinfo);
terminate();
}
And the function in the other class I am passing it to:
1 2 3 4
void GameComponent::Update(const tm* eventTime)
{
cout << "ID: " << id << "/tUpdated at: " << asctime(eventTime) << endl;
}
When I run this, it comes up with the "Break or Continue" error due to an unhandled exception, and an access violation.
However, when I run this code (in just the Game::Run function)
1 2 3 4 5 6 7 8 9 10 11
void Game::Run()
{
initialise();
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
cout << "current time is: " << asctime(timeinfo) << endl;
//components[0]->Update(timeinfo);
terminate();
}
It prints the date and time perfectly fine. I thought it was as easy as just passing timeinfo as a parameter to the Update function, but it just doesn't like it.
Please can anyone tell me where I'm going wrong?
Thanks!
Dec 3, 2014 at 1:15pm UTC
Does components[0] exist and is it pointing to a valid object?
Dec 3, 2014 at 1:30pm UTC
Yes, although I've just figured out the problem..
The error was actually because of the "id" variable in the GameComponent::Update function.
Which is NOW what I need help with!
The "id" variable here in the .cpp file for GameComponent class:
1 2 3 4
void GameComponent::Update(const tm* eventTime)
{
cout << "ID: " << id << "/tUpdated at: " << asctime(eventTime) << endl;
}
Is apparently an unhandled exception and an access violation of the variable that has been put in the .h file for the same GameComponent class here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#pragma once
#include <ctime>
#include <stdlib.h>
#include <iostream>
using namespace std;
class GameComponent
{
public :
GameComponent();
void Update(const tm* eventTime);
private :
int id = 0;
int instances = 0;
};
Any ideas why it would be throwing up an error? I tried also putting
int id = 0;
as public, but still no luck!
Dec 3, 2014 at 1:57pm UTC
Add these two lines, and then try debugging it.
1 2 3 4 5 6
void GameComponent::Update(const tm* eventTime)
{
std::cout << "this points to object at address: " << this << '\n' << std::flush; // *** added
std::cout << "this->instances == " << this ->instances << '\n' << std::flush; // *** added
cout << "ID: " << id << "/tUpdated at: " << asctime(eventTime) << endl;
}
Dec 3, 2014 at 2:05pm UTC
Thanks for the reply, however it didn't work! :( Just came up with the same error!
If it helps, here's the whole code for the .h and the .cpp file:
.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#pragma once
#include <ctime>
#include <stdlib.h>
#include <iostream>
using namespace std;
class GameComponent
{
private :
int id;
int instances;
public :
GameComponent();
void Update(const tm* eventTime);
};
.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "stdafx.h"
#include "GameComponent.h"
GameComponent::GameComponent()
{
}
void GameComponent::Update(const tm* eventTime)
{
//std::cout << "this points to object at address: " << this << '\n' << std::flush;
//std::cout << "this->instances == " << this->instances << '\n' << std::flush;
cout << "ID: " << id << "Updated at: " << eventTime->tm_hour << ":" << eventTime->tm_min << ":" << eventTime->tm_sec << endl;
}
The error that I'm getting is:
Unhandled exception at 0x00CD6DAB in CourseW1.exe: 0xC0000005: Access violation reading location 0xCDCDCDD1.
Topic archived. No new replies allowed.