首页 > 解决方案 > 我收到错误“'Schedule::Schedule (){} 的重新定义”

问题描述

我得到错误:

Schedule.cc:8:1:错误:重新定义“Schedule::Schedule()”8 | 时间表::时间表 (){} | ^~~~~~~~ 编译由于 -Wfatal-errors 而终止。

我已经研究了几个小时关于头卫的研究,尽管我已经使用了它们(imo以正确的方式),但我仍然认为这可能是问题所在。

日期.h

    #ifndef DATES_INCL
#define DATES_INCL

#include <string>

class Dates {

public:

        char findFormat (std::string input_date);
        bool isLeapYear (int year);
        std::string getDaysPeriod (std::string input_date);
        std::string getYearPeriod (std::string input_date);
        std::string getDaysDash (std::string input_date);
        std::string getYearDash (std::string input_date);
        std::string getMonthDash (std::string input_date);
        void getDatesFileI (char *buffer, std::string filename);
        void getDatesFile (const char *fstring, char *buffer, std::string filename);
        void getDates (const char *fstring, char *buffer);
        std::string collectArgs (char *argv[], int argc);
        std::string getDate(std::string date);

private:
        std::string pname;
};

#endif

事件.h

    #ifndef EVENT_INCL
#define EVENT_INCL

#include <string>
#include <iostream>

class Event {

public:
        Event (char cstr[]);
        Event (std::string date);
        Event (const Event&); //Copy Ctor
        Event &operator= (const Event&); //Assignment Operator
        ~Event(); //Dtor
        void set (int year, int month, int day);
        int year () const;
        int month () const;
        int day () const;

private:
        std::string date;
        int eyear, emonth, eday;
};

        std::ostream &operator<< (std::ostream&, const Event&);

#endif

时间表.h

    #ifndef SCHEDULE_INCL
#define SCHEDULE_INCL

#include "Event.h"
#include "Dates.h"
#include <iostream>
#include <vector>

class Schedule {

public:
        Schedule () = delete;
        Schedule (std::istream &stream);
        Schedule (char cstring[]);
        Schedule (std::string str);
        Schedule (const Schedule&) = default; //Copy Ctor
        Schedule &operator= (const Schedule&); //Assignment Operator
        ~Schedule ();
        void read (std::istream &stream);
        void clear () const;
        size_t size () const;
        bool empty () const;
        Schedule &operator[] (int);

private:
        std::vector<std::string> dates;

};

        std::ostream &operator<< (std::ostream&, const Schedule&);

#endif

附表.cc

    #include "Schedule.h"
#include "Event.h"
#include "Dates.h"
#include <string>

using namespace std;

Schedule::Schedule (){}

Schedule::Schedule (istream &stream){
        //read from stream
}

Schedule::Schedule (char cstr[]){
        string str(cstr);
        //str is fielname -- .read
}

Schedule::Schedule (string str){
        //str is a filename -- .read from file name
}

Schedule::Schedule (const Schedule &sched){

}

Schedule &Schedule::operator= (const Schedule &sched){

}

Schedule::~Shedule(){}

void Schedule::read (istream &stream){

}

void Schedule::clear (){
        this->dates.clear();
}

size_t Schedule::size (){
        return this->dates.size();
}

bool Schedule::empty (){
        if (this->dates.empty()){
                return true;
        }
        return false;
}

string &Schedule::operator[] (int index){
        return this->dates[index];
}

ostream &operator<< (ostream &out, const Schedule &sched){
        //print all events in this schedule     
}

任何帮助将不胜感激!

标签: c++

解决方案


您将默认构造函数声明为已删除Schedule.h

Schedule () = delete;

但是然后有一个定义

Schedule::Schedule (){}

Schedule.cc. 取决于你想要什么

  • 不要定义已删除的构造函数(Schedule::Schedule (){}从中删除Schedule.cc
  • 或者如果您想要一个默认构造函数,请不要将其标记为已删除并将定义保留在Schedule.cc.
  • 或者,由于您的默认构造函数没有任何自定义逻辑,请删除定义并将声明更改Schedule.h

    Schedule() = default;
    

    并让编译生成一个默认构造函数。

第二个和第三个选项是相同的,只是第三个选项更简单。


推荐阅读