I need some help with C++ vectors. I have an object which has two attributes, both constant references (this means that I don't have an empty constructor for the class). I need to create a vector with some of these objects and I need to sort them based on the value of one of the attributes (specifically the otherObj attribute, which class has all the operators overloaded).
I'm having some trouble because, when I try to do a simple std::sort on the vector, I think it tries to create another object of that class with an empty constructor to swap those objects, so I don't know what to do.
I paste an example code of how I basically have these objects (I may have made some errors)
class Object{
const OtherObject& otherObj;
const int& myInt;
Object(const OtherObject& o, const int& i){
this->otherObj = o;
this->myInt = i;
}
};
void myFunction(std::vector<OtherObject>& vec){
int i1 = 1;
int i2 = 2;
int i3 = 3;
Object obj1(vec.at(0), i1);
Object obj2(vec.at(1), i2);
Object obj3(vec.at(2), i3);
std::vector<Object> myVec {obj1, obj2, obj3};
//Sort the vector here
}