Sergej Fomin
Sergej Fomin

Reputation: 2002

STABLE_PARTITION problems: no matching function to call to "swap"

somehow I can't use stable_partition algorithm on

vector<pair<Class, string>>. 

I can re-organize the code to get what I want, but for me (as I am new to C++) it's more "WHY" and not "HOW" question. will be glad if you clarify this behavior:

First, class Date (you can omit it and come look at it later):

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <vector>

using namespace std;

class Date {
public:
    Date(int new_year, int new_month, int new_day) {
        year = new_year;    month = new_month;      day = new_day;
      }

    int GetYear() const {return year;}
    int GetMonth() const {return month;}
    int GetDay() const {return day;}

private:
  int year, month, day;
};

bool operator<(const Date& lhs, const Date& rhs) {
  return vector<int>{lhs.GetYear(), lhs.GetMonth(), lhs.GetDay()} <
      vector<int>{rhs.GetYear(), rhs.GetMonth(), rhs.GetDay()};
}

bool operator==(const Date& lhs, const Date& rhs) {
  return vector<int>{lhs.GetYear(), lhs.GetMonth(), lhs.GetDay()} ==
      vector<int>{rhs.GetYear(), rhs.GetMonth(), rhs.GetDay()};
}

SO THIS IS THE CLASS WITH THE TROUBLE:

class Database {
public:
    void Add(const Date& date, const string event){
            storage.push_back(make_pair(date, event));
            set_dates.insert(date);

    }


    void Print(ostream& s) const{

        for(const auto& date : set_dates) {
// TROUBLE IS HERE:
            auto it = stable_partition(begin(storage), end(storage),
                [date](const pair<Date, string> p){
                return p.first == date;
            });

        };

}

private:
      vector<pair<Date, string>> storage;
      set<Date> set_dates;

};

When compiled, it returns a lot of problems of same kind: enter image description here

I've tried the same code on vector<pair<int, string>> (used stable_partition with lambda {return p.first == _int; } and it worked.

Would appreciate your help

Upvotes: 0

Views: 457

Answers (2)

Max Langhof
Max Langhof

Reputation: 23701

The std::stable_partition function is supposed to modify the vector. However, you are calling it in a const member function, so storage is const there. This can't work.

Solution: Don't make Print const, or use std::stable_partition on a copy of storage. Neither is a great solution, so you should probably rethink your design.

Upvotes: 3

Loc Tran
Loc Tran

Reputation: 1180

You need to define overloading operator= for Date class as well. It will work if you do that stuff.

class Date {
public:
Date(int new_year, int new_month, int new_day) {
    year = new_year;    month = new_month;      day = new_day;
  }

// Need to define overloading operator=
Date& operator=(const Date& rhs)
{

}

int GetYear() const {return year;}
int GetMonth() const {return month;}
int GetDay() const {return day;}

private:
  int year, month, day;
};

Upvotes: 1

Related Questions