【问题标题】:"member function already defined or declared" - What does that mean?“成员函数已经定义或声明” - 这是什么意思?
【发布时间】:2015-07-18 12:21:45
【问题描述】:

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

我做的最后一件事是添加“按日期排序”功能。在此之前一切正常。如果我现在运行我的代码,我会收到以下错误消息(我收到此消息 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();
  • 指南要求你先提取一个最小的例子,这也是有原因的。它迫使您专注于相关部分,并且在很多情况下您会自己发现错误。然后,在此处发布最少量的内联代码,因为 Pastbin 的生命周期有限,并且所有信息都应在此处。

标签: c++ sorting visual-c++ compiler-errors


【解决方案1】:

它在第 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);
}

【讨论】:

  • 这将更难帮助,因为它是一个运行时问题。检查第 120 到 130 行周围的 *_vec.erase。您可能传递了一个无效的迭代器,可能试图擦除比向量中更多的内容。
猜你喜欢
  • 2020-05-14
  • 2014-09-20
  • 2022-12-05
  • 2013-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多