I want to sort a vector according to the custom datatype. I followed Sorting a vector of custom objects answer. I am using lambda function to compare the objects. However I am getting compiler errors as following while sorting:
/usr/include/c++/7/bits/stl_algo.h:1852: error: cannot bind non-const lvalue reference of type 'MyData&' to an rvalue of type 'std::remove_reference::type {aka MyData}' *__first = _GLIBCXX_MOVE(__val); ^
main.cpp
#include "mydata.h"
#include <vector>
int main()
{
std::vector<MyData> tv {MyData(2,21), MyData(3,20), MyData(10,100), MyData(9,20)};
std::sort(tv.begin(), tv.end(), []( MyData const& lhs, MyData const& rhs ){
return lhs.get_size() < rhs.get_size();
});
return 0;
}
mydata.cpp
#ifndef MYDATA_H
#define MYDATA_H
#include <iostream>
#include <algorithm>
class MyData
{
private:
int *m_data;
int m_x;
size_t m_size;
public:
MyData(const size_t &size,int const &x):
m_data(new int[size]),
m_x(x),
m_size(size)
{
std::fill_n(m_data,m_size, m_x);
std::cout << *m_data << " ctor" << m_size << std::endl;
}
MyData(const MyData& other):
m_data(new int[other.m_size]),
m_x(other.m_x),
m_size(other.m_size)
{
std::fill_n(m_data,m_size, m_x);
std::cout << *m_data << " cctor" << m_size << std::endl;
}
MyData& operator=(MyData& other)
{
std::cout << *m_data << " cbctor" << m_size << std::endl;
swap(*this,other);
std::cout << *m_data << " cactor" << m_size << std::endl;
return *this;
}
~MyData(){
std::cout << *m_data << " dtor" << m_size << std::endl;
delete[] m_data;
}
size_t get_size() const{
return m_size;
}
friend void swap(MyData& first, MyData& second){ // (1)
std::swap(first.m_size, second.m_size);
std::swap(first.m_x, second.m_x);
std::swap(first.m_data, second.m_data);
}
friend std::ostream& operator<< (std::ostream& stream, const MyData& mydata) {
stream << *(mydata.m_data) << " " << mydata.m_size << " "<< mydata.m_x;
return stream;
}
};
#endif // MYDATA_H
I do not understand the error. I am not changing the value of the reference, why I am getting this error. I also read this but did not understand why it is occurring here. Thank you.
MyData a(...), b(...);thena = bwould swapaandb, butbis not expected to change. If you want to useswapin your assignment operator, pass the argument by value. Should incidentally solve your problem as well.swapwithother? It should absolutely not modify the rhsMyData& operator=(MyData other)solved the problem. However I dont know if using swap is best in this case. I followed some blogs were they say using swap will make the assignment operator exception free.m_dataavector<int>. The assignment operator should include a test forthis == &otherto do nothing in the case of self assignment.