I have a simple class and I am using a std::vector to contain all of the instances of that class. However when I do:
std::vector<MyType> v;
MyType m(1, 2, 3);
v.push_back(m);
Or if I do:
v.push_back(new MyType(1, 2, 3);
I get this error:
error C2664: 'void std::vector<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'MyType *' to 'MyType &&'
And if it is important, here my MyType code:
class MyType
{
public:
int a;
int b;
float c;
MyType(int A, int B, float C)
{
a = A;
b = B;
c = C;
}
};
I don’t understand what is going wrong.
EDIT: My original (first snippet) of code works. For whatever reason I had to rebuild the solution and afterwards it compiled fine.
In your code,
MyTypeis a type, whilemis an object, sonew m(1,2,3)doesn’t even make sense.Write:
Instead of
push_back, you could also useemplace_backas :emplace_backis preferable.