I have two class templates MyClassA<T>
and MyClassB<T>
.
From these, I have constructed two std::vector
's as std::vector<MyClassA<double>> A
and std::vector<MyClassB<double>> B
.
My goal is to first sort A
in ascending order (in actual I will do a range/partial sort).
Then using that order to sort B
.
What I am doing so far is the following:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <random>
// my class definitions
template<typename T>
class MyClassA
{
public:
T valA;
};
template<typename T>
class MyClassB
{
public:
T valB;
};
// my compare class
template<typename T>
using TIter = typename std::vector<T>::const_iterator;
template <typename T>
class MyCompare
{
public:
bool operator()(std::pair<std::size_t, TIter<MyClassA<T>>>
const& a, std::pair<std::size_t, TIter<MyClassA<T>>> const& b)
{
return *(a.second).valA < *(b.second).valA;
}
};
// sort from given order
//... not yet implemented
int main()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(0, 1);
// first ClassA Object vector A
std::vector<MyClassA<double>> A(5);
for(auto& i:A) i.valA = dis(gen);
// second ClassB Object vector B
std::vector<MyClassB<double>> B(5);
for(auto& i:B) i.valB = dis(gen);
// sort vector A elements' references in ascending order
std::size_t i = 0;
std::vector<std::pair<std::size_t, TIter<MyClassA<double>>>> torder(A.size());
for(auto it = A.begin(); it != A.end(); ++it, ++i) torder[i] = std::make_pair(i, it);
std::sort(torder.begin(), torder.end(), MyCompare<double>()); // getting error here
// sort vectors A and B elements using the above sorted order
// ...
return 0;
}
However, I am getting the following error:
error: 'const class __gnu_cxx::__normal_iterator<const MyClassA<double>*, std::vector<MyClassA<double> > >' has no member named 'valA'
Aucun commentaire:
Enregistrer un commentaire