Advertisement

expression must have class type

Started by May 25, 2020 02:20 AM
6 comments, last by Alberth 4 years, 6 months ago

I am working on a AABB collision detection function. I am getting a “expression must have class type” on my “one” variable in my checkCollision function. I have googled this error and have looked at other sites but with no luck on finding an answer. I am learning about pointers and passing by reference. here is my short piece of code.

#include<iostream>

using namespace std;

bool CheckCollision(GameObject &one, GameObject &two); // AABB - AABB collision

class GameObject
{
public:
	int Position;
	int Size;
	int x;
	int y;
};

int main()
{ 
	GameObject one,two;
	one.x = 0;
	one.y = 0;
	one.Position = 0;
	one.Size = 0;
	two.x = 0;
	two.y = 0;
	two.Position = 0;
	two.Size = 0;

	cout << CheckCollision(one, two) << endl;

	system("pause");
	return 0;
}

bool CheckCollision(GameObject &one, GameObject &two) // AABB - AABB collision
{
	// collision x-axis?
	bool collisionX = one.Position.x + one.Size.x >= two.Position.x &&
		two.Position.x + two.Size.x >= one.Position.x;
	// collision y-axis?
	bool collisionY = one.Position.y + one.Size.y >= two.Position.y &&
		two.Position.y + two.Size.y >= one.Position.y;
	// collision only if on both axes
	return collisionX && collisionY;
}

No, you are getting “expression must have class type” errors on the Position and Size members of your one variable. The way you declared it, Position is a plain old integer, so “one.Position.x” doesn't make sense. What's the x of 5?

Advertisement

I took out size and position variables and get more errors. maybe I am not understanding light breeze comment

Maybe so, Phil. Nobody can help you if you don't clearly state exactly what errors you're getting now. And as always, you must clearly state a question, too.

-- Tom Sloper -- sloperama.com

You wrote

one.Position = 0;

in your program.

You also wrote

one.Position.x

in your ‘collisionX’ line in CheckCollision().

The question is, what is the value of “one.Position.x”, given that “one.Position” is 0 .

You probably should also explain how you reached the answer.

I am getting an error “class GameObject has no member Size, and class GameObject has no member Position, when I put these variables back into the class I still get an error of my ”one" variable which is “expression must have class type” I have tried initializing a “class one” but I still get the same error.

Advertisement

Yep, you can try the same thing forever, you will consistently get the same error until you fix it.

That in turn means you have to investigate what exactly is wrong. The question that ‘a light breeze’ asked is a spoiler to put you on the right track. I tried to make it more clear, but that doesn't seem to work either.

So what is wrong with the question? You cannot find the phrases I quoted, or you don't understand what it means, or you don't understand what an integer is, or ???

Generally a compiler also gives you a line number with it, and often also a column, does that help?

This topic is closed to new replies.

Advertisement