"member function already defined or declared" - 这是什么意思?

"member function already defined or declared" - What does that mean?

本文关键字:是什么 意思 or member function already defined declared      更新时间:2023-10-16

我正在写一个程序,它应该列出一些任务,按日期排序等等。

我做的最后一件事是添加"按日期排序"功能。在那之前一切都很好。如果我现在运行我的代码,我得到以下错误消息(我得到这个消息3次)

member function already defined or declared
我不明白,出什么事了。触发错误的代码如下所示:
static bool compareDates(entry e1, entry e2) { 
    string s1 = e1.date;
    string s2 = e2.date;
    int day_1 = atoi(s1.substr(0, 2).c_str());
    int month_1 = atoi(s1.substr(3, 2).c_str()); // dd.mm.yyyy
    int year_1 = atoi(s1.substr(6, 4).c_str());
    int day_2 = atoi(s2.substr(0, 2).c_str());
    int month_2 = atoi(s2.substr(3, 2).c_str());
    int year_2 = atoi(s2.substr(6, 4).c_str());
    if (year_1 > year_2) return true;
    else if (year_1 < year_2) return false;
    if (month_1 > month_2) return true;
    else if (month_1 < month_2) return false;
    if (day_1 > day_2) return true;
    else if (day_1 < day_2) return false;
    return true;
}
// ... some code in between ...
private: void sortList() { // in the class
    sort(john_lines.begin(), john_lines.end(), compareDates);
    sort(tomas_lines.begin(), tomas_lines.end(), compareDates);
    sort(bernd_lines.begin(), bernd_lines.end(), compareDates);
    sort(peter_lines.begin(), peter_lines.end(), compareDates);
}

我试着在没有其他代码的情况下运行这段代码,它工作了。有人知道我的申请有什么问题吗?下面是我收到的错误信息:

Error 1 error C2535: 'void V2::MainWindow::sortList(void)' : member function already defined or declared
<path>MainWindow.h 422 1 V2
Error 14 error C2535: 'void V2::MainWindow::sortList(void)' : member function already defined or declared
<path>MainWindow.h 422 1 V2
Error 28 error C2535: 'void V2::MainWindow::sortList(void)' : member function already defined or declared
<path>MainWindow.h 422 1 V2
下面是我的代码:
#include <vector>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <exception>
#include <algorithm>
#include <stdio.h>
using namespace std;
struct entry {
    string text;
    string date;
    bool finished;
};
vector< entry > john_lines;
bool compareDates(entry e1, entry e2) {
    string s1 = e1.date;
    string s2 = e2.date;
    int day_1 = atoi(s1.substr(0, 2).c_str());
    int month_1 = atoi(s1.substr(3, 2).c_str()); // dd.mm.yyyy
    int year_1 = atoi(s1.substr(6, 4).c_str());
    int day_2 = atoi(s2.substr(0, 2).c_str());
    int month_2 = atoi(s2.substr(3, 2).c_str());
    int year_2 = atoi(s2.substr(6, 4).c_str());
    if (year_1 > year_2) return true;
    else if (year_1 < year_2) return false;
    if (month_1 > month_2) return true;
    else if (month_1 < month_2) return false;
    if (day_1 > day_2) return true;
    else if (day_1 < day_2) return false;
    return true;
}
int main() {    
    entry e;
    e = { "clean the window", "12.08.2016", true };
    john_lines.push_back(e);
    e = { "tidy the room", "14.06.2012", false };
    john_lines.push_back(e);
    e = { "sort the papers", "16.08.2016", false };
    john_lines.push_back(e);
    e = { "writing the code for this application", "19.08.2018", false };
    john_lines.push_back(e);
    e = { "test period of this applicaition", "30.11.2020", false };
    john_lines.push_back(e);
    cout << "-------------------------------------------------------------------------------" << endl;
    cout << "- before:                                                                     -" << endl;
    cout << "-------------------------------------------------------------------------------" << endl;
    for(int i=0; i<john_lines.size(); i++) {
        e = john_lines.at(i);
        string finished = (e.finished) ? "(  done  ) " : "(not done) ";
        cout << finished << e.date << " - " << e.text << endl;
    }
    cout << endl << endl;
    sort(john_lines.begin(), john_lines.end(), compareDates);
    cout << "-------------------------------------------------------------------------------" << endl;
    cout << "- after:                                                                      -" << endl;
    cout << "-------------------------------------------------------------------------------" << endl;
    for(int i=0; i<john_lines.size(); i++) {
        e = john_lines.at(i);
        string finished = (e.finished) ? "(  done  ) " : "(not done) ";
        cout << finished << e.date << " - " << e.text << endl;
    }
}

在第83行声明,可能在其他地方定义。

private: void sortList();

您已经在第422行重新定义了它

private: void sortList() {
        sort(john_lines.begin(), john_lines.end(), compareDates);
        sort(tomas_lines.begin(), tomas_lines.end(), compareDates);
        sort(bernd_lines.begin(), bernd_lines.end(), compareDates);
        sort(peter_lines.begin(), peter_lines.end(), compareDates);
}