"member function already defined or declared" - 这是什么意思?
"member function already defined or declared" - What does that mean?
我正在写一个程序,它应该列出一些任务,按日期排序等等。
我做的最后一件事是添加 "sort by date" 功能。在此之前一切正常。如果我现在 运行 我的代码,我会收到以下错误消息(我收到此消息 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);
}
我正在写一个程序,它应该列出一些任务,按日期排序等等。
我做的最后一件事是添加 "sort by date" 功能。在此之前一切正常。如果我现在 运行 我的代码,我会收到以下错误消息(我收到此消息 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);
}