I get an error when trying to get value from the class in another class.

Sep 3, 2022 at 10:41am
Hello. I get an error when trying to get value from another class which are also in one big class. Error: Type name is not allowed.
Here is the actual code:
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
namespace vector2d
{
	class fvector
	{
	public:
		class pixelQuantity
		{
			float px_quantity = 0.0f;
			float px_quantity_x_cycle = 0.0f;
			float px_quantity_y_cycle = 0.0f;
		};
		class transform
		{
			class position
			{
				float x1 = 0.0f;
				float y1 = 0.0f;
				float x2 = 0.0f;
				float y2 = 0.0f;
				//Calculate X Center of a vector
				float centerx()
				{
					return (x2 - x1) / 2.0f;
				}
				//Calculate Y Center of a vector
				float centery()
				{
					return (y2 - y1) / 2.0f;
				}
			};
			class rotation
			{
				float angle = 0.0f;
				pivot pivot_rotation = center;
				rotation_direction_ rotation_direction = clockwise;
			};
			class size
			{
				int thickness_l = 1; //thickness left
				int thickness_r = 1; //thickness right
		                //Calculate Vector's magnitude
				float magnitude()
				{
					float adx = fabs(position.x2 - position.x1);
					float ady = fabs(position.y2 - position.y1);
					return sqrt(adx * adx + ady * ady);
				}
			};
		};

Error is in the magnitude() function and when i create an object vector2d::fvector v1() and try to access v1.transform i also get an error that Type name is not allowed.
Last edited on Sep 3, 2022 at 11:25am
Sep 3, 2022 at 11:14am
That is unreadable! Please post the actual code and the compiler provided errors.
Sep 3, 2022 at 11:16am
IMHO, bitmap images are atrocious.

Do you really need nested class definitions?


You have:
1
2
3
4
5
6
7
8
9
class A {
  float x;
};

class B {
  float func() {
    return A.x; // error: type name not allowed
  }
};

That error is obvious. The A is a type, just like int. You can't write:
1
2
int = 3;
std::cout << (int + 39);

You have to write:
1
2
int y = 3;
std::cout << (y + 39);

Similarly,
1
2
3
A aaa;
A bbb;
A ccc[3];

The aaa, bbb, ccc[0], ccc[1], and ccc[2] are objects of type A. Each of these objects has member x.
You could write:
1
2
3
4
5
class B {
  float func(A y) {
    return y.x;
  }
};

Now there is instance y of A with concrete x.

That would still give an error, because x is a private member of type A, but that you can solve by improving the interface of A.
Sep 3, 2022 at 11:38am
Thanks keskiverto! I experimented with your solution to this problem and everything worked fine!
Topic archived. No new replies allowed.