can't access instantiated class member

why cant it find orientation.xrot below after instatiation?

1
2
3
4
5
6
7
8
9
10
11
12
namespace FPS_cam
{
		class orientation{
			public:
			float xrot = 0, yrot = 0; // zrot = 0;
			};
		class position{
			public:
			float xpos = 0,ypos = 0,zpos = 0;
			};

...

main..
1
2
FPS_cam::orientation* pOrientation = new FPS_cam::orientation();
std::cout<<FPS_cam::orientation.xrot; // 


error:

rc\main.cpp:112:37: error: expected primary-expression before '.' token
std::cout<<FPS_cam::orientation.xrot;
Of which object? The dot is member access operator.
In foo.bar, the foo is the name of a variable.
The type of foo is something that has member named bar.

FPS_cam::orientation is not an object. It is the name of a type.

If you have pointer named 'gaz', you can reach the pointed to object via dereference operator * :
*gaz
then you can use the member access operator . :
(*gaz).bar

However, there is a second member access operator that slightly simplifies the code, the -> :
gaz->bar
Thx, that was it. Sort of related question:

If I am calling a value for example from one class to another before instantiation there is the possibility that that value is a member of an alias object not the same as hard coded when I instantiate it. How do you code for the fact that you might not know what the object being called is named until run time? Is that the 'this pointer'? How do you avoid hard-coding that object.


¿what language are you talking about?
I cannot grasp the meaning of «before instantiation», «value is a member of an alias», «not know the name of the object».
I agree with @ne555. Your question does not make much sense to me, either. Are you trying to ask about referencing members of objects/references/pointers which are arguments to a function?

I would suggest that you write some sample code that demonstrates what you are trying to ask about. Then, with a concrete example we can make sense of the question.


As far as the "this" pointer, see the following tutorial page:

http://www.cplusplus.com/doc/tutorial/templates/

(This is actually the "Classes II" page, even though it is named "templates". You might want to look at all of the Classes pages from http://www.cplusplus.com/doc/tutorial/ to get a better understanding of how classes work.)
How do you code for the fact that you might not know what the object being called is named until run time?


At runtime it doesn't have a name.
In a very garbled way I was asking about ways to make global variables class based with a global object pointer. I didn't realize at the moment that reading from many sources GLUT programming simply demands global variables and functions. Furthermore its near impossible to bring in vars outside of glut prototypes. I don't mean to sound professorial about all this, because you all know better than me. So what I did was make a global variables class and so far its worked great.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
namespace FPS_cam
{
	class global_vars{
	public:
		float xpos = 0, ypos = 0, zpos = 0, angle=0.0;
		float xrot = 0, yrot = 0;
		float lastx, lasty;
		//positions of the cubes
		float positionz[10];
		float positionx[10];
		};
	global_vars gvInstance;
	global_vars * pGv = &gvInstance;
...
etc.
 float xrotrad, yrotrad;
yrotrad = (pGv->yrot / 180 * 3.141592654f);
 xrotrad = (pGv->xrot / 180 * 3.141592654f);
pGv->xpos += float(sin(yrotrad)) ;
pGv->zpos -= float(cos(yrotrad)) ;
pGv->ypos -= float(sin(xrotrad)) ;

etc.

I'm now of course able to access the global_vars with pGv without trying to pass it as a variable or fx that's not allowed as an argument in the prototype function.

Do I need to destroy the ptr at end?

Thank you
Last edited on
Do I need to destroy the ptr at end?


You need to delete anything you created using new.

Otherwise, no.
Is this set of definitions (lines 2 - 14) in a header file? If so, you could run into the problem of having a gvInstance and a pGv instantiated in every .cpp file that includes that header file. In that case, you won't actually have a global structure. Instead, you will have a collection of local structures.

You need to declare the pGv as external in the header file, and then instantiate gvInstance and assign pGv in a source file somewhere in order to get a global variable.

If this is all within a single compilation unit (source file), then lines 13 and 14 should be declared static.
Topic archived. No new replies allowed.