这是我的Calendar.cpp文件。
#include <iostream>
#include <string>
#include <sstream>
#include "Calendar.h"
char months[12][10] = { "January", "February", "March","April","May","June","July","August","September","October","November","December" };
问题是在类声明中显示出来。
class Date {
private:
int dd, mm, yy;
public:
Date() {
dd = 1; mm = 1; yy = 1900;
}
Date(int m, int d, int y) {
dd = d; mm = m; yy = y;
bool check = checkdate();
if (!check) {
cout << "Invalid date give. Resetting to default" << endl;
dd = mm = 1; yy = 1900;
}
}
bool checkdate() {
bool leap = checkleapyear();
if (mm>12 || mm <= 0) //check months
return false;
if ((mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12) && dd>31)
return false;
else if (leap && mm == 2 && dd>29)
return false;
else if (mm == 2 && dd>28)
return false;
else if (dd>30)
return false;
return true;
}
bool checkleapyear() {
if ((yy % 400 == 0) || (yy % 4 == 0 && yy % 100 != 0))
return true;
else
return false;
}
string toString() {
stringstream ss;
ss << months[mm - 1] << " " << dd << "," << yy;
return ss.str();
}
Date nextDate() {
int y, d, m;
if (mm == 2) {
bool flag = checkleapyear();
if (flag) {
if (dd >= 29) {
d = 1;
m = 3;
}
else
d++;
}
else {
if (dd >= 28) { d = 1; m = 3; }
else
d++;
}
y = yy;
return Date(m, d, y);
}
if ((mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10) && dd>31)
{
m++;
d = 1;
y = yy;
}
else if (mm == 12) { m = 1; y = yy++; d = 1; }
else if (dd>30) {
m++;
d = 1;
y = yy;
}
else {
d = dd + 1;
m = mm;
y = yy;
}
return Date(m, d, y);
}
void compareDates(Date &d)
{
if (d.yy>this->yy)
cout << "The first date comes before the second date" << endl;
else if (d.yy<this->yy)
cout << "The second date comes before the first date" << endl;
else if (d.yy == this->yy) {
if (d.mm>this->mm)
cout << "The first date comes before the second date" << endl;
else if (d.mm<this->mm)
cout << "The second date comes before the first date" << endl;
else {
if (d.dd>this->dd)
cout << "The first date comes before the second date" << endl;
else if (d.dd<this->dd)
cout << "The second date comes before the first date" << endl;
else {
cout << "The two dates are equal" << endl;
}
}
}
}
};
这是我的Calendar.h文件。
#ifndef CALENDAR_H
#define CALENDAR_H
#include <iostream>
#include <string>
using namespace std;
class Date {
private:
int dd, mm, yy;
public:
Date();
Date(int m, int d, int y);
bool checkdate();
bool checkleapyear();
string toString();
Date nextDate();
void compareDates(Date &d);
};
#endif
我的Calendar.cpp文件中出现错误 - '“Date”:'class'type redefinition“。我试图改变类中的一些东西以使其工作但似乎还有一些其他问题。
答案 0 :(得分:0)
由于您已在Calender.h文件中声明了您的类,因此您在Calender.cpp中的实现实际上是在重新定义您的类。正确的方法是使用范围解析运算符' :: '
引用该类您的Calender.cpp文件应如下所示:
#include <iostream>
#include <string>
#include <sstream>
#include "Calendar.h"
char months[12][10] = { "January", "February", "March","April","May","June","July","August","September","October","November","December" };
// your default no-args class constructor
Date::Date(){
dd = 1; mm = 1; yy = 1900;
}
/*
Another Way you could implement your constructor is by using initialisation lists (check our the second link in my answer to know more)
Date::Date(int m, int d, int y) : mm(m), dd(d), yy(y) { }
*/
Date::Date(int m, int d, int y) {
// your constructor code here
}
bool Date::checkdate(){
// your implementation here
}
bool Date::checkleapyear(){
// your implementation here
}
string Date::toString(){
// your implementation here
}
Date Date::nextDate(){
// your implementation here
}
void Date::compareDates(Date &d){
// your implementation here
}
就是这样。它确实有助于将您的类声明和实现分开。
请查看以下链接,以帮助您更好地理解:
http://www.learncpp.com/cpp-tutorial/89-class-code-and-header-files/
http://www.cppforschool.com/tutorial/separate-header-and-implementation-files.html