Visual C ++:“已定义或声明的成员函数” - 如何,这是什么意思?

时间:2015-07-18 12:21:45

标签: c++ sorting compiler-errors

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

我做的最后一件事是添加“按日期排序”功能。在此之前一切正常。如果我现在运行我的代码,我收到以下错误消息(我收到此消息3次)

member function already defined or declared

我不明白,有什么不对。触发错误的代码看起来像这样(之后我会在pastebin上发布完整的代码;你也应该知道,这是德国的日期格式,这意味着,day.month.year)

static bool compareDates(entry e1, entry e2) { // I also tried without "static"; this part of the code is above the class
    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 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 BüroPlaner_V2::MainWindow::sortList(void)' : member function already defined or declared c:\users\phantom6208\documents\visual studio 2013\projects\büro planer_v2\büro planer_v2\MainWindow.h   422 1   Büro Planer_V2

错误14错误C2535:'voidBüroPlaner_V2:: MainWindow :: sortList(void)':已定义或声明的成员函数c:\ users \ phantom6208 \ documents \ visual studio 2013 \ projects \büroplaner_v2\büroplaner_v2\ MainWindow .h 4221BüroPlaner_V2

错误28错误C2535:'voidBüroPlaner_V2:: MainWindow :: sortList(void)':已定义或声明的成员函数c:\ users \ phantom6208 \ documents \ visual studio 2013 \ projects \büroplaner_v2\büroplaner_v2\ MainWindow .h 4221BüroPlaner_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;
    }
}

所有代码:

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <dirent.h>
#include <vector>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <exception>
#include <algorithm>
#include <functional>
#include "AddWindow.h"

#pragma once

namespace BüroPlaner_V2 {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace std;

    struct entry {
        string text;
        string date;
        bool finished;
    };

    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;
    }

    static vector< string > john_vec;
    static vector< string > tomas_vec;
    static vector< string > peter_vec;
    static vector< string > bernd_vec;

    static vector< entry > john_lines;
    static vector< entry > tomas_lines;
    static vector< entry > peter_lines;
    static vector< entry > bernd_lines;

    public ref class MainWindow : public System::Windows::Forms::Form
    {
    private: AddWindow^ addWindow;

    private: bool strtobool(string s) {
        if (s == "true")  return true;
        else return false;
    }
    public:
        MainWindow(void)
        {
            InitializeComponent();

            loadContent();
            drawToList();
        }
    private: void loadContent() {
        john_vec.clear();
        tomas_vec.clear();
        peter_vec.clear();
        bernd_vec.clear();

        john_lines.clear();
        tomas_lines.clear();
        peter_lines.clear();
        bernd_lines.clear();

        for (int i = 0; i < 4; i++) {
            string person;
            switch (i) {
                case 0: person = "john"; break;
                case 1: person = "tomas"; break;
                case 2: person = "peter"; break;
                case 3: person = "bernd"; break;
            }

            DIR *dir;
            struct dirent *ent;
            if ((dir = opendir(("c:\\bueroplaner\\" + person + "\\").c_str())) != NULL) {
                while ((ent = readdir(dir)) != NULL) {
                    string line(ent->d_name);
                    switch (i) {
                    case 0: john_vec.push_back(line); break;
                    case 1: tomas_vec.push_back(line); break;
                    case 2: peter_vec.push_back(line); break;
                    case 3: bernd_vec.push_back(line); break;
                    }
                }
                closedir(dir);
            }
        }

        john_vec.erase(john_vec.begin());
        john_vec.erase(john_vec.begin());

        tomas_vec.erase(tomas_vec.begin());
        tomas_vec.erase(tomas_vec.begin());

        peter_vec.erase(peter_vec.begin());
        peter_vec.erase(peter_vec.begin());

        bernd_vec.erase(bernd_vec.begin());
        bernd_vec.erase(bernd_vec.begin());

        for (int i = 0; i < 4; i++) {
            int count = 0; string person;
            switch (i) {
            case 0: count = john_vec.size(); person = "john"; break;
            case 1: count = tomas_vec.size(); person = "tomas"; break;
            case 2: count = peter_vec.size(); person = "peter"; break;
            case 3: count = bernd_vec.size(); person = "bernd"; break;
            }

            for (int j = 0; j < count; j++) {
                ifstream ifs;

                string path;

                switch (i) {
                case 0: path = "c:/bueroplaner/john/" + john_vec.at(j);  break;
                case 1: path = "c:/bueroplaner/tomas/" + tomas_vec.at(j); break;
                case 2: path = "c:/bueroplaner/peter/" + peter_vec.at(j); break;
                case 3: path = "c:/bueroplaner/bernd/" + bernd_vec.at(j); break;
                } ifs.open(path);

                try {
                    string lines[3];
                    for (int h = 0; h < 3; h++) {
                        string line;
                        getline(ifs, line);

                        lines[h] = line;
                    }

                    entry e = { lines[0], lines[1], strtobool(lines[2]) };

                    switch (i) {
                        case 0: john_lines.push_back(e); break;
                        case 1: tomas_lines.push_back(e); break;
                        case 2: peter_lines.push_back(e); break;
                        case 3: bernd_lines.push_back(e); break;
                    }
                }
                catch (...) { MessageBox::Show("file unreadable or wrong syntax!"); }

                ifs.close();
            }
        }

    }

    private: void drawToList() {
        this->john_list->Items->Clear();
        for (int i = 0; i < john_lines.size(); i++) {
            string tf = (john_lines.at(i).finished) ? "  done  " : "not done";
            String^ str = gcnew String(("(" + tf + ") " + john_lines.at(i).date + " - " + john_lines.at(i).text).c_str());
            this->john_list->Items->Add(str);
        }
        this->tomas_list->Items->Clear();
        for (int i = 0; i < tomas_lines.size(); i++) {
            string tf = (tomas_lines.at(i).finished) ? "  done  " : "not done";
            String^ str = gcnew String(("(" + tf + ") " + tomas_lines.at(i).date + " - " + tomas_lines.at(i).text).c_str());
            this->tomas_list->Items->Add(str);
        }
        this->peter_list->Items->Clear();
        for (int i = 0; i < peter_lines.size(); i++) {
            string tf = (peter_lines.at(i).finished) ? "  done  " : "not done";
            String^ str = gcnew String(("(" + tf + ") " + peter_lines.at(i).date + " - " + peter_lines.at(i).text).c_str());
            this->peter_list->Items->Add(str);
        }
        this->bernd_list->Items->Clear();
        for (int i = 0; i < bernd_lines.size(); i++) {
            string tf = (bernd_lines.at(i).finished) ? "  done  " : "not done";
            String^ str = gcnew String(("(" + tf + ") " + bernd_lines.at(i).date + " - " + bernd_lines.at(i).text).c_str());
            this->bernd_list->Items->Add(str);
        }
    }
    protected: ~MainWindow()
        {
            saveStuff();
            if (components) { delete components; }
        }

    private: System::Windows::Forms::TabControl^  tabControl1;
    private: System::Windows::Forms::TabPage^  john;
    private: System::Windows::Forms::TabPage^  bernd;
    private: System::Windows::Forms::TabPage^  tomas;
    private: System::Windows::Forms::TabPage^  peter;
    private: System::Windows::Forms::ListBox^  bernd_list;


    private: System::Windows::Forms::ListBox^  tomas_list;
    private: System::Windows::Forms::ListBox^  peter_list;
    private: System::Windows::Forms::ListBox^  john_list;
    private: System::Windows::Forms::Button^  addEntry;
    private: System::Windows::Forms::Button^  removeEntry;
    private: System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        void InitializeComponent(void)
        {
            this->tabControl1 = (gcnew System::Windows::Forms::TabControl());
            this->john = (gcnew System::Windows::Forms::TabPage());
            this->john_list = (gcnew System::Windows::Forms::ListBox());
            this->bernd = (gcnew System::Windows::Forms::TabPage());
            this->bernd_list = (gcnew System::Windows::Forms::ListBox());
            this->tomas = (gcnew System::Windows::Forms::TabPage());
            this->tomas_list = (gcnew System::Windows::Forms::ListBox());
            this->peter = (gcnew System::Windows::Forms::TabPage());
            this->peter_list = (gcnew System::Windows::Forms::ListBox());
            this->addEntry = (gcnew System::Windows::Forms::Button());
            this->removeEntry = (gcnew System::Windows::Forms::Button());
            this->tabControl1->SuspendLayout();
            this->john->SuspendLayout();
            this->bernd->SuspendLayout();
            this->tomas->SuspendLayout();
            this->peter->SuspendLayout();
            this->SuspendLayout();
            // 
            // tabControl1
            // 
            this->tabControl1->Controls->Add(this->john);
            this->tabControl1->Controls->Add(this->bernd);
            this->tabControl1->Controls->Add(this->tomas);
            this->tabControl1->Controls->Add(this->peter);
            this->tabControl1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
                static_cast<System::Byte>(0)));
            this->tabControl1->ItemSize = System::Drawing::Size(118, 25);
            this->tabControl1->Location = System::Drawing::Point(-1, -1);
            this->tabControl1->Name = L"tabControl1";
            this->tabControl1->Padding = System::Drawing::Point(50, 3);
            this->tabControl1->SelectedIndex = 0;
            this->tabControl1->Size = System::Drawing::Size(578, 258);
            this->tabControl1->TabIndex = 0;
            this->tabControl1->TabStop = false;
            // 
            // john
            // 
            this->john->Controls->Add(this->john_list);
            this->john->Location = System::Drawing::Point(4, 29);
            this->john->Name = L"john";
            this->john->Padding = System::Windows::Forms::Padding(3, 3, 3, 3);
            this->john->Size = System::Drawing::Size(570, 225);
            this->john->TabIndex = 0;
            this->john->Text = L"John";
            this->john->UseVisualStyleBackColor = true;
            // 
            // john_list
            // 
            this->john_list->Font = (gcnew System::Drawing::Font(L"Lucida Console", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
                static_cast<System::Byte>(0)));
            this->john_list->FormattingEnabled = true;
            this->john_list->ItemHeight = 16;
            this->john_list->Location = System::Drawing::Point(-4, 0);
            this->john_list->Name = L"john_list";
            this->john_list->Size = System::Drawing::Size(578, 228);
            this->john_list->TabIndex = 1;
            // 
            // bernd
            // 
            this->bernd->Controls->Add(this->bernd_list);
            this->bernd->Location = System::Drawing::Point(4, 29);
            this->bernd->Name = L"bernd";
            this->bernd->Padding = System::Windows::Forms::Padding(3, 3, 3, 3);
            this->bernd->Size = System::Drawing::Size(570, 225);
            this->bernd->TabIndex = 1;
            this->bernd->Text = L"Bernd";
            this->bernd->UseVisualStyleBackColor = true;
            // 
            // bernd_list
            // 
            this->bernd_list->Font = (gcnew System::Drawing::Font(L"Lucida Console", 12));
            this->bernd_list->FormattingEnabled = true;
            this->bernd_list->ItemHeight = 16;
            this->bernd_list->Location = System::Drawing::Point(-4, 0);
            this->bernd_list->Name = L"bernd_list";
            this->bernd_list->Size = System::Drawing::Size(578, 228);
            this->bernd_list->TabIndex = 0;
            // 
            // tomas
            // 
            this->tomas->Controls->Add(this->tomas_list);
            this->tomas->Location = System::Drawing::Point(4, 29);
            this->tomas->Name = L"tomas";
            this->tomas->Size = System::Drawing::Size(570, 225);
            this->tomas->TabIndex = 2;
            this->tomas->Text = L"Tomas";
            this->tomas->UseVisualStyleBackColor = true;
            // 
            // tomas_list
            // 
            this->tomas_list->Font = (gcnew System::Drawing::Font(L"Lucida Console", 12));
            this->tomas_list->FormattingEnabled = true;
            this->tomas_list->ItemHeight = 16;
            this->tomas_list->Location = System::Drawing::Point(-4, 0);
            this->tomas_list->Name = L"tomas_list";
            this->tomas_list->Size = System::Drawing::Size(578, 228);
            this->tomas_list->TabIndex = 2;
            // 
            // peter
            // 
            this->peter->Controls->Add(this->peter_list);
            this->peter->Location = System::Drawing::Point(4, 29);
            this->peter->Name = L"peter";
            this->peter->Size = System::Drawing::Size(570, 225);
            this->peter->TabIndex = 3;
            this->peter->Text = L"Peter";
            this->peter->UseVisualStyleBackColor = true;
            // 
            // peter_list
            // 
            this->peter_list->Font = (gcnew System::Drawing::Font(L"Lucida Console", 12));
            this->peter_list->FormattingEnabled = true;
            this->peter_list->ItemHeight = 16;
            this->peter_list->Location = System::Drawing::Point(-4, 0);
            this->peter_list->Name = L"peter_list";
            this->peter_list->Size = System::Drawing::Size(578, 228);
            this->peter_list->TabIndex = 2;
            // 
            // addEntry
            // 
            this->addEntry->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 11.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
                static_cast<System::Byte>(0)));
            this->addEntry->Location = System::Drawing::Point(12, 266);
            this->addEntry->Name = L"addEntry";
            this->addEntry->Size = System::Drawing::Size(259, 40);
            this->addEntry->TabIndex = 2;
            this->addEntry->Text = L"Aufgabe Hinzufügen";
            this->addEntry->UseVisualStyleBackColor = true;
            this->addEntry->Click += gcnew System::EventHandler(this, &MainWindow::addEntry_Click);
            // 
            // removeEntry
            // 
            this->removeEntry->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 11.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
                static_cast<System::Byte>(0)));
            this->removeEntry->Location = System::Drawing::Point(304, 266);
            this->removeEntry->Name = L"removeEntry";
            this->removeEntry->Size = System::Drawing::Size(259, 40);
            this->removeEntry->TabIndex = 3;
            this->removeEntry->Text = L"Aufgabe Erledigt";
            this->removeEntry->UseVisualStyleBackColor = true;
            this->removeEntry->Click += gcnew System::EventHandler(this, &MainWindow::removeEntry_Click);
            // 
            // MainWindow
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(575, 312);
            this->Controls->Add(this->addEntry);
            this->Controls->Add(this->removeEntry);
            this->Controls->Add(this->tabControl1);
            this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
            this->MaximizeBox = false;
            this->MinimizeBox = false;
            this->Name = L"MainWindow";
            this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
            this->Text = L"Büro Planer";
            this->tabControl1->ResumeLayout(false);
            this->john->ResumeLayout(false);
            this->bernd->ResumeLayout(false);
            this->tomas->ResumeLayout(false);
            this->peter->ResumeLayout(false);
            this->ResumeLayout(false);

        }
#pragma endregion

public: void refresh() { loadContent(); drawToList(); }

private: System::Void addEntry_Click(System::Object^  sender, System::EventArgs^  e) {
    AddWindow^ frm = gcnew AddWindow(tabControl1->SelectedIndex, this);
    frm->ShowDialog();
}
private: System::Void removeEntry_Click(System::Object^  sender, System::EventArgs^  e) {
    if (tabControl1->SelectedIndex == 0) {
        if (john_list->SelectedIndex != -1 && john_lines.size() >= john_list->SelectedIndex + 1)
            john_lines.at(john_list->SelectedIndex).finished = true;
    }
    if (tabControl1->SelectedIndex == 1) {
        if (bernd_list->SelectedIndex != -1 && bernd_lines.size() >= bernd_list->SelectedIndex + 1)
            bernd_lines.at(bernd_list->SelectedIndex).finished = true;
    }
    if (tabControl1->SelectedIndex == 2) {
        if (tomas_list->SelectedIndex != -1 && tomas_lines.size() >= tomas_list->SelectedIndex + 1)
            tomas_lines.at(tomas_list->SelectedIndex).finished = true;
    }
    if (tabControl1->SelectedIndex == 3) {
        if (peter_list->SelectedIndex != -1 && peter_lines.size() >= peter_list->SelectedIndex + 1)
            peter_lines.at(peter_list->SelectedIndex).finished = true;
    }

    saveStuff();
    drawToList();
}
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);
}
private: void saveStuff() {
    for (int i = 0; i < john_lines.size(); i++) {
        entry e = john_lines.at(i);
        ofstream ofs("c:/bueroplaner/john/" + john_vec.at(i), ios::out);

        string finished = (e.finished) ? "true" : "false";

        ofs << e.text << endl;
        ofs << e.date << endl;
        ofs << finished << endl;

        ofs.close();
    }
    for (int i = 0; i < tomas_lines.size(); i++) {
        entry e = tomas_lines.at(i);
        ofstream ofs("c:/bueroplaner/tomas/" + tomas_vec.at(i), ios::out);

        string finished = (e.finished) ? "true" : "false";

        ofs << e.text << endl;
        ofs << e.date << endl;
        ofs << finished << endl;

        ofs.close();
    }
    for (int i = 0; i < bernd_lines.size(); i++) {
        entry e = bernd_lines.at(i);
        ofstream ofs("c:/bueroplaner/bernd/" + bernd_vec.at(i), ios::out);

        string finished = (e.finished) ? "true" : "false";

        ofs << e.text << endl;
        ofs << e.date << endl;
        ofs << finished << endl;

        ofs.close();
    }
    for (int i = 0; i < peter_lines.size(); i++) {
        entry e = peter_lines.at(i);
        ofstream ofs("c:/bueroplaner/peter/" + peter_vec.at(i), ios::out);

        string finished = (e.finished) ? "true" : "false";

        ofs << e.text << endl;
        ofs << e.date << endl;
        ofs << finished << endl;

        ofs.close();
    }
}
};
}

1 个答案:

答案 0 :(得分: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);
}