Hi guys,

I'm having trouble using a pointer to a vector<float>, it doesn't seems to be responding as I'm expecting it to.

Probably a silly mistake on my behalf, but would anyone kindly point it out please.

vector<float>* t_matrix;
...
cout << "get_t_matrices 0: " << Character::anim.get_animations()[j].get_t_matrices()[0].size() << endl;
t_matrix = &(Character::anim.get_animations()[j].get_t_matrices()[0]);
cout << "t_matrix size: " << (t_matrix)->size() << endl;

get_t_matrices() returns a vector<vector<float>>

Line 3 outputs "get_t_matrices 0: 16" to the console.
But line 5 outputs "t_matrix size: 0".

Can anyone explain why t_matrix doesn't seem to be pointing to the value on line 4, please?

Hi guys,

I'm having trouble using a pointer to a vector<float>, it doesn't seems to be responding as I'm expecting it to.

Probably a silly mistake on my behalf, but would anyone kindly point it out please.

vector<float>* t_matrix;
...
cout << "get_t_matrices 0: " << Character::anim.get_animations()[j].get_t_matrices()[0].size() << endl;
t_matrix = &(Character::anim.get_animations()[j].get_t_matrices()[0]);
cout << "t_matrix size: " << (t_matrix)->size() << endl;

get_t_matrices() returns a vector<vector<float>>

Line 3 outputs "get_t_matrices 0: 16" to the console.
But line 5 outputs "t_matrix size: 0".

Can anyone explain why t_matrix doesn't seem to be pointing to the value on line 4, please?

Can we see the implementations of get_t_matirices, get_animations, and the Character class please, or at least those bits of them pertinent to this problem?

get_t_matrices() is a member function of the Animation class.

vector<vector<float>> Animation::get_t_matrices()
{
	return Animation::t_matrices;
}

Animation::t_matrices is defined as:

std::vector<std::vector<float>> t_matrices;

get_animations()

vector<Animation>& Character_Animation::get_animations()
{
	return Character_Animation::bone_animations;
}

Character_Animation::bone_animations:

std::vector<Animation> bone_animations;

Character class:

class Character {
public:
    // ATTRIBUTES
        Character_Animation anim;

Hope this is all the information required.

Is your code exactly as posted? I've just knocked up the following test which works fine.

#include <vector>
#include <iostream>


struct A
{
   std::vector<std::vector<float> > a_matrix_;
   std::vector<std::vector<float> >& get_matrix() { return A::a_matrix_;}
};

struct B
{
   std::vector<A> a_vector_;
   std::vector<A>& get_a_vec() { return B::a_vector_; }
};


int main() 
{
   std::vector<float> fvec;
   fvec.push_back( 10.2f );
   fvec.push_back( 10.3f );
   fvec.push_back( 10.4f );

   A a;
   a.a_matrix_.push_back(fvec);
   a.a_matrix_.push_back(fvec);
   a.a_matrix_.push_back(fvec);

   B b;
   b.a_vector_.push_back( a );
   
   std::vector<float>* a_matrix;
   a_matrix = &(b.get_a_vec()[0].get_matrix()[0]);
   size_t s = (a_matrix)->size();
   std::cout << "Size - " << (a_matrix)->size() << std::endl;

   return 0;
}

It's for intents and purposes as written. The "..." in the original post represents code which doesn't do anything to t_matrix, and lines 3-5 of original are exacty copies of code, there isn't anything inbetween them, so j isn't changing, or any of the matrices. That's what's confusing me, it doesn't look to me like I have a bug in one of the functions, because

cout << "get_t_matrices 0: " << Character::anim.get_animations()[j].get_t_matrices()[0].size() << endl;

and

t_matrix = &(Character::anim.get_animations()[j].get_t_matrices()[0]);

are looking at exactly the same thing.


The following works fine:

vector<float> t_matrix
...
t_matrix = (Character::anim.get_animations()[j].get_t_matrices()[0]);

So it appears to me that I am implementing the pointers incorrectly, rather than the get_ functions. Although it works as above, I'm using this code for 3D graphics and I need to keep processing time to a minimum as it will be done every frame, and copying vectors. So I could really do with getting this working with pointers.

Your use of the pointer looks fine to me. Without you posting working code which exhibits the problem I cannot see anything wrong with it.

Try to produce a cut down, but compilable set of classes, as I have done in my example, which displays your problem.

Anyway, you are returning by reference anyway so why bother converting that reference to a pointer?

Why not simply use it directly?

std::vector<float>& a_matrix = b.get_a_vec()[0].get_matrix()[0];

or

cout << (b.get_a_vec()[0].get_matrix()[0]).size();

Although get_t_matrices() returns by reference, is "t_matrix = ..." not making a fresh copy of one of it's elements?

t_matrix = (Character::anim.get_animations()[j].get_t_matrices()[0]);

Although get_t_matrices() returns by reference, is "t_matrix = ..." not making a fresh copy of one of it's elements?

t_matrix = (Character::anim.get_animations()[j].get_t_matrices()[0]);

No. not if t_matrix is defined as a reference. e.g.
vector<float>& t_matrix...

Here you are returning a reference which is essentially an alias for the object in question, in this case an element of your matrix vector. Note that references, unlike a pointer, cannot be unbound so you have to set this in the declaration, as per my previous post, and they cannot be rebound, unlike a pointer, where you can set the pointer to point to object A say and then reset it to point to B. If you require behaviour of this type then you may need to use a pointer. But in that case then why not write your member function to return a pointer type?

Just to clarify, i'll quote this line from the nearest C++ text I have to hand...

'When an object is passed as a reference parameter, the objects value is not copied. Rather, the address of the object being passed is copied. Each access of the reference parameter blah, blah, blah' but you get the picture...

Edited 6 Years Ago by mattjbond: n/a

Still don't understand why my pointers weren't working, but seems I didn't need them in the first place. Thanks for the help =)

Still don't understand why my pointers weren't working, but seems I didn't need them in the first place. Thanks for the help =)

nor do I. and without seeing your full code I can't speculate as to why. If you like the info posted then vote up and mark thread solved.

This question has already been answered. Start a new discussion instead.