I'm writing a small openGL program to do keyframe animation. The lines of code I have highlighted in my code appear responsible for the error I get when compiling (full output below).
I'd like to know why this is wrong, and also any suggestions as to how to do it differently.
class Vector3f {
float _item[3];
public: //.......
// Vector3f is just a simple array of floats with a rotation function.
// I don't think it's relevant to this problem.
}
struct Keyframe{
int frameid;
Vector3f rotation;
};
struct AnimTrack {
int trackid;
vector <Keyframe> _keyFrames;
};
class Animation {
int numFrames;
int currentFrame;
int numJoints;
vector <AnimTrack> _tracks;
public:
Animation(){};
void setNumFrames(int frames){ numFrames = frames;}
int getNumFrames() {return numFrames;}
int getnumJoints(){return numJoints;}
void addAnimtrack(AnimTrack &track){
_tracks.push_back(track);
}
};
I changed the operator as you suggested, and it works fine now. I'm still not entirely sure why the const makes such a difference, so i'm back to my books to find out.
Because there is a little clause in the standard that says you cannot bind temporaries to non-const references. A simple example:
1 2 3 4 5 6 7 8 9 10 11 12
#include <string>
#include <iostream>
usingnamespace std;
void print_me( string& s ) {
cout << s << endl;
}
int main() {
print_me( string( "Hello World" ) );
}
generates a compile error because the string( "Hello World" ) is a temporary object that is being passed to a function that accepts a (non-const)
reference. To fix the compile error, print_me should take its parameter by const
reference. The standard allows for temporaries to be bound to const references.