gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:09:53 26 4
gpt4 key购买 nike

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

我做的最后一件事是添加“按日期排序”功能。在此之前一切正常。如果我现在运行我的代码,我会收到以下错误消息(我收到此消息 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);
}

关于c++ - "member function already defined or declared"- 这是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31491106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com