views:

119

answers:

2

Honestly, I couldnt think of a better title for this issue because I am having 2 problems and I don't know the cause.

The first problem I have is this

//global declaration
float g_posX = 0.0f;
.............


//if keydown happens
g_posX += 0.03f;


&m_mtxView._41 = g_posX;

I get this error

cannot convert from 'float' to 'float *'

So I assume that the matrix only accepts pointers. So i change the varible to this....

//global declaration
float *g_posX = 0.0f;
.............


//if keydown happens
g_posX += 0.03f;


&m_mtxView._41 = &g_posX;

and I get this error

cannot convert from 'float' to 'float *'

which is pretty much saying that I can not declare g_posX as a pointer.

honestly, I don't know what to do.

+5  A: 

1.)

m_mtxView._41 = g_posX;

2.)

Update: this piece of code is quite unnecessary, although it shows how to use a pointer allocated on the heap.

float* g_posX = new float; // declare a pointer to the address of a new float
*g_posX = 0.0f; // set the value of what it points to, to 0.0This
m_mtxView._41 = *g_posX; // set the value of m_mtxView._41 to the value of g_posX
delete g_posX; // free the memory that posX allocates.

Hint: Read "*" as "value of" and "&" as "address of"

Viktor Sehr
It's hard to see what the point of the second would be.
Matthew Flaschen
It's also hard to see how the second `float` would ever be freed.
sbi
updated the answer
Viktor Sehr
Thanks. I get lost with the understanding of these pointers sometimes.
numerical25
@Viktor: And after your update the code is such that `m_mtxView._41` points to a `float` that got deleted.
sbi
@sbi: Uhm, no, m_mtxView._41 is not a pointer.
Viktor Sehr
@Viktor: Yeah, sorry, my brainfart. Anyway, the whole dynamic memory thing is - pardon my French - stupid. 2) buys nothing over 1) but opportunity for errors - especially for someone who's still confused about pointers.
sbi
@sbi: Simple examples can be used as demonstrations for more complex goals. The fact that *this* example is "useless" on its own does not mean that the concept of dynamic memory management is useless.
dash-tom-bang
@sbi; might be, I just modified his code to show how he should write to make it work
Viktor Sehr
+1  A: 

Why are you trying to take the address of m_mtxView._41? What's wrong with m_mtxView._41 = g_posX;?

sbi
I get this error cannot convert from 'float' to 'float *'
numerical25
@numerical25: I thought this happened with `
sbi