I’m sure it has been asked 100 times, but push_back is such a popular question that I couldn’t find an answer after searching for a long time.
My problem is that on this page it says that that a recursive push_back should work perfectly without any problem on multiple levels.
vector< vector<int> > vI2Matrix; // Declare two dimensional array
vector<int> A, B;
A.push_back(10);
A.push_back(20);
A.push_back(30);
B.push_back(100);
B.push_back(200);
B.push_back(300);
vI2Matrix.push_back(A);
vI2Matrix.push_back(B);
However in my version I am trying to use a custom class Vector3f instead of int. I would naturally think that the following code would work, but it doesn’t. It works on the 1st level, but not on the 2nd.
vector< vector<Vector3f> > m;
vector<Vector3f> a;
a.push_back(Vector3f(1,2,3)); // <- 1st level works
m.push_back(a); // <- 2nd level doesn't
The error code returned is:
gobase.h:343: error: no match for ‘operator=’ in ‘* __result = * __first’
demo1.h:38: note: candidates are: Vector3f& Vector3f::operator=(Vector3f&)
The Vector3f class is defined in an external header file, what I am not supposed to modify. Do you think there is something missing from the given header file what makes me unable to use push_back?
The following functions are defined in the header file:
class Vector3f {
float _item[3];
public:
float & operator [] (int i)
Vector3f(float x, float y, float z)
Vector3f()
Vector3f & operator = (Vector3f & obj)
Vector3f & operator += (Vector3f & obj)
bool operator ==(Vector3f & obj)
}
Update
Here is the & operator = from the header file:
Vector3f & operator = (Vector3f & obj)
{
_item[0] = obj[0];
_item[1] = obj[1];
_item[2] = obj[2];
return *this;
};
Can you tell me how should I modify this to make push_back work?
Have a look at the complete steps required to solve this problem in GCC, I posted it as an additional answer.
The assignment operator is missing from your Vector3f class. Template expansion of vector’s push_back needs an assignment operator to copy the value into the vector, so the compiler complains that it cannot find it. If you get to modify that header, you will also need to define a
Vector3f(cont Vector3f& other)copy constructor.