I need help

how to do this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

class B
{
   public:
      double Pos;
      double Size;
      double x;
};

B b1;

int main()
{
    int Test = b1.Pos.x;
    cout << Test << endl;
    return 0;
}


i get error : request for member 'x' in 'b1.B::Pos', which is of non-class type 'double'
Last edited on
You've defined B::Pos as a double. How can it possibly have a member x?

Which data member is it that you actually want to access? Is it B::Pos, or B::x?
Last edited on
Last edited on
What are you trying to do here? Pos is a double, a decimal number, so b1.Pos.x does not make sense, as you are accessing the member Pos of b1 (b1.Pos), and then trying to access a member of Pos called x (b1.Pos.x). But Pos doesn't have members because it's a double, just a number. Additionally, you are not actually doing anything to Pos or x before printing them, so what is the purpose of this program? Finally, as is customary to point out, using namespace std; is really not a good habit to get into, so it would be better to simply put std::cout and std::endl, you're not even saving yourself keystrokes by having using namespace std; in this code.

EDIT: Probably should have refreshed the page before answering.
Last edited on
Um... I'm not sure what you're asking. Please read what I wrote and, in particular, note that the last part of my answer was a question.

Also, please don't create duplicate threads for the same question. It wastes peoples' time.

http://www.cplusplus.com/forum/general/196167/
Ok
closed account (z05DSL3A)
You would need to be looking at something more like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>


struct Size
{
	double x = 0;
	double y = 0;
};

struct Point
{
	double x = 0;
	double y = 0;
};

struct Box
{
	Point position;
	Size size;
};

bool CheckCollision(Box &one, Box &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;
}


int main()
{
	Box box;
	Box box2;

	box.position.x = 1;
	box.position.y = 1;
	box.size.x = 2;
	box.size.y = 2;

	box2.position.x = 2;
	box2.position.y = 2;
	box2.size.x = 2;
	box2.size.y = 2;

	if (CheckCollision(box, box2) == true)
	{
		std::cout << "Collision" << std::endl;
	}


    return 0;
}
Thanks:)
Topic archived. No new replies allowed.