lundi 23 août 2021

C++ template function in class in a .cpp file [duplicate]

I am trying to make a class Database with template functions RemoveIf and FindIf. When I try to compile the code this error shows. The FindIf and RemoveIf functions should recieve an iterator and iterate threw the map<Date, vector> and return all the string that are needed. I don't think the Date class is needed to solve the problem, but if you think so I will edit the question and add it. Can someone please tell me what's wrong?

In file included from C:\Users\gof\CLionProjects\untitled\main.cpp:1:
C:\Users\gof\CLionProjects\untitled\database.h:17:9: error: 'int Database::RemoveIf(It&) [with It = main()::<lambda(const Date&, const string&)>]', declared using local type 'main()::<lambda(const Date&, const string&)>', is used but never defined [-fpermissive]
     int RemoveIf(It& it);
         ^~~~~~~~
C:\Users\gof\CLionProjects\untitled\database.h:21:20: error: 'std::vector<std::__cxx11::basic_string<char> > Database::FindIf(It&) [with It = main()::<lambda(const Date&, const string&)>]', declared using local type 'main()::<lambda(const Date&, const string&)>', is used but never defined [-fpermissive]
     vector<string> FindIf(It& it);
                    ^~~~~~
C:\Users\gof\CLionProjects\untitled\database.h:21:20: warning: 'std::vector<std::__cxx11::basic_string<char> > Database::FindIf(It&) [with It = main()::<lambda(const Date&, const string&)>]' used but never defined
C:\Users\gof\CLionProjects\untitled\database.h:17:9: warning: 'int Database::RemoveIf(It&) [with It = main()::<lambda(const Date&, const string&)>]' used but never defined
     int RemoveIf(It& it);
         ^~~~~~~~

database.h:

#pragma once
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <string>
#include "date.h"

using namespace std;


class Database {
public:
    void Add(const Date& date, const string& event);

    template <typename It>
    int RemoveIf(It& it);


    template <typename It>
    vector<string> FindIf(It& it);

    string Last(Date d);

    void Print(ostream& os) const;

private:
    map<Date, vector<string>> storage;
};

database.cpp:

#include "database.h"

void Database::Add(const Date& date, const string& event) {
    for (const auto& i : storage[date]) {
        if (i == event) {
            return ;
        }
    }
    storage[date].push_back(event);
}

template <typename It>
int Database::RemoveIf(It& it) {
    int count = 0;
    auto _end =
            [&](map<Date, vector<string>>& s) {
                for (auto i : s) {
                    for (int j = 0; j < i.second.size(); j++) {
                        if (it(i.first, i.second[j])) {
                            i.second.erase(i.second.begin() + j);
                            count++;
                        }
                    }
                }
            };
    _end(storage);
    return count;
}

template <typename It>
vector<string> Database::FindIf(It& it) {
    ostringstream s;
    vector<string> Str;
    for (auto i : storage) {
        for (int j = 0; j < i.second.size(); j++) {
            if (it(i.first, i.second[j])) {
                s << i.first << ' ' << i.second[j];
                Str.push_back(s.str());
                s.clear();
            }
        }
    }
    return Str;
}

string Database::Last(Date d) {
    ostringstream os;
    bool b = true;
    int day, month, year;
    for (auto i : storage) {
        if (d < i.first) {
            break;
        }
        day = i.first.GetDay();
        month = i.first.GetMonth();
        year = i.first.GetYear();
        b = false;
    }
    if (b) {
        throw invalid_argument("");
    }
    Date date(year, month, day);
    os << date << ' ' << storage[date].back();
    return os.str();
}

void Database::Print(ostream& os) const {
    for (const auto& item : storage) {
        for (const string& event : item.second) {
            os << item.first << " " << event << endl;
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire