0

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'

2
  • How are you doing the sorting, std::sort? Commented Jun 21, 2016 at 1:59
  • @ThomasMcLeod: If you have asked about sorting vector A, then I have edited above. Yes, std::sort will actually not sort std::vector A itself, but it will sort the references only. (Good question) Commented Jun 21, 2016 at 2:55

2 Answers 2

4

It's a simple case of problem with the operator precedence. The member selection dot . has higher precedence than the dereference operator, so e.g. *(a.second).valA is parsed as *((a.second).valA).

Simply change to e.g. a.second->valA (or (*a.second).valA).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. Hell I wasted so much time!
1
return *(a.second).valA < *(b.second).valA;

a.second and b.second appears to be iterators, so this should simply be:

return (a.second)->valA < (b.second)->valA;

1 Comment

Thanks a lot. Hell I wasted so much time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.