c++ - 我收到错误“'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
}
任何帮助将不胜感激!
解决方案
您将默认构造函数声明为已删除Schedule.h
Schedule () = delete;
但是然后有一个定义
Schedule::Schedule (){}
在Schedule.cc
. 取决于你想要什么
- 不要定义已删除的构造函数(
Schedule::Schedule (){}
从中删除Schedule.cc
) - 或者如果您想要一个默认构造函数,请不要将其标记为已删除并将定义保留在
Schedule.cc
. 或者,由于您的默认构造函数没有任何自定义逻辑,请删除定义并将声明更改
Schedule.h
为Schedule() = default;
并让编译生成一个默认构造函数。
第二个和第三个选项是相同的,只是第三个选项更简单。
推荐阅读
- c# - 验证电话号码
- excel - 如果语句在 ByVal Target As Range 中未正确执行
- python - 返回 sklearn 的高斯朴素贝叶斯的原始概率
- javascript - 如何在使用 Javascript 在智能手机上录制时捕获所有频率?
- go - 通过 flag.PrintDefaults() 设置输出顺序
- r - 2项支撑计算
- c# - 使用 C# Interop 获取所有 MSACCESS 应用程序实例
- javascript - 如何在 saveChanges 上捕获错误 - Google 应用制作工具
- python - 从python中的文本中提取电话号码
- node.js - Node Js Server 启动但在执行另一个文件之后