Data structure and array problem

Started by
1 comment, last by DividedByZero 8 years, 6 months ago
Hi Guys,

I am having a bit of a problem with an array of data structures.

The custom data structure is;


struct Float6
{
	float x0;
	float y0;
	float x1;
	float y1;
	float x2;
	float y2;
};
I am initialising the structure like this;


Float6 collisionMap[] =
{
	(0.0f, -7.72136f, 0.0f, 0.0f, 56.0f, -7.72136f),
	(0.0f, 0.0f, 0.151287f, 0.049888f, 56.0f, -7.72136f)
};
When I read the first line back it keeps returning -7.72136

This is how I am reading back;


		std::string szBuffer = "";
		std::stringstream ss;
		ss << collisionMap[0].x0;
		szBuffer += "[0].x0 = ";
		szBuffer += ss.str();

		ss << collisionMap[0].y0;
		szBuffer += "   [0].y0 = ";
		szBuffer += ss.str();

		ss << collisionMap[0].x1;
		szBuffer += "   [0].x1 = ";
		szBuffer += ss.str();

		ss << collisionMap[0].y1;
		szBuffer += "   [0].y1 = ";
		szBuffer += ss.str();

		ss << collisionMap[0].x2;
		szBuffer += "   [0].x2 = ";
		szBuffer += ss.str();

		ss << collisionMap[0].y2;
		szBuffer += "   [0].y2 = ";
		szBuffer += ss.str();

		MessageBox(0, szBuffer.c_str(), "Data", 0);

Any assistance as to why I am getting the same value (-7.72136) in every position would be greatly appreciated.
Advertisement
Took me a moment to realize what was going on, my mind kept interpreting the syntax I expected to see, rather than reading the syntax that was actually there. That happens every so often when debugging.
.
Float6 collisionMap[] =
{
	(0.0f, -7.72136f, 0.0f, 0.0f, 56.0f, -7.72136f), //<--- Your brackets are (...), when they should be {...}
	(0.0f, 0.0f, 0.151287f, 0.049888f, 56.0f, -7.72136f)
};
In your initialization, you are accidentally using parentheses instead of curly brackets.
This means it's evaluating the entire set of parentheses as a single statement, like (a + b + c).

Because the comma operator is a valid operator, it's using the comma operator to calculate the statement.
The comma operator calculates and discards whatever is on the left-hand side, and just returns what's on the right-hand side.

This means what you are actually assigning is:
Float6 collisionMap[] =
{
	(ignore, ignore, ignore, ignore, ignore, -7.72136f),
	(ignore, ignore, ignore, ignore, ignore, -7.72136f)
};
.
Which basically means you are doing:
Float6 collisionMap[] =
{
	Float6{-7.72136f},
	Float6{-7.72136f}
}; 
.
Welcome to C++! laugh.png
Arrggh, noob mistake!

Thanks heaps smile.png

This topic is closed to new replies.

Advertisement