This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I have a pair of functions within a class ("Vehicle"), one of which changes the values of some variables, and the other which then uses these variables. The first function may be called several times before the second is used, as only when certain conditions are satisfied for every member of group of objects (a std::list of Vehicles) is the second function called to use the calculated values.
However, the values set in function one are not the values that function two is reading - for example: Function One - variable forePosition is set to 0.1008 every time. Function Two - variable forePosition is read as 1.83671e-040 (i.e. some random uninitialised value) every time.
However, upon checking another variable set from a parameter during the object's construction (i.e. it should be different between different objects), both functions report the same value, meaning they must be the same object (or, possibly, copies of the same object).
I've put together two code excerpts: Train.cpp calls the two Vehicle functions within the Train::moveTrain() function. Vehicle.cpp is the problem class, with the variables set in checkMovement() (which are set by the calls to canAxleMove(), for example float newForePosition) being different to those read in doMovement(). The output from the functions is currently:
checkMovement function
std::cout << this->reportingMark << "\n";
std::cout << forePosition << " | " << newForePosition << "\n";
>Foobazbar
>0.1 / 0.1008
doMovement function
std::cout << this->reportingMark << " DoMove\n";
std::cout << forePosition << " | " << newForePosition << "\n";
>Foobazbar DoMove
>0.1 | 1.83671e-040
Whilst "Foobazbar" (a string variable, unique to each object) is identical in both, newForePosition (a float variable) changes from 0.1008 to 1.8E-40
Edit: After many more tries, I have completely gutted the checkMovement function and removed the calls do the doMovement and canAxleMove functions, leaving:
Class Vehicle
{
public:
bool checkMovement(float distanceToMove, float* obstacleDistance);
private:
bool areNewPositionsSet;
}
bool Vehicle::checkMovement(float distanceToMove, float* obstacleDistance)
{
std::cout << &areNewPositionsSet << " " << areNewPositionsSet << " Start\n";
areNewPositionsSet = true;
std::cout << &areNewPositionsSet << " " << areNewPositionsSet << " Finish\n";
return areNewPositionsSet;
}
The output for this when looping is:
0x28b5ae 235 Start
0x28b5ae 1 Finish
0x28b5ae 235 Start
0x28b5ae 1 Finish
Subreddit
Post Details
- Posted
- 10 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/cpp_questio...