我正在尝试为格里高利日期计算器创建一个复制构造函数。
myDate.cpp (minimal)
#include "stdafx.h"
#include "myDate.h"
int month = 0, day = 0, year = 0;
myDate::myDate()
{
month = 5; day = 11; year = 1959; // Default date
}
myDate::myDate(int M, int D, int Y)
{
month = M; day = D; year = Y;
}
myDate::myDate(const myDate& D)
{
month = D.month;
day = D.day;
year = D.year;
}
我得到的错误'月'不是'myDate'的成员(白天和年份相同),而“myDate”类没有成员“月”(白天和年份相同)。创建复制构造函数的正确语法是什么?这是我见过广泛使用的语法。非常感谢任何帮助!
以下是完整的标题:
myDate.h
#ifndef MYDATE_H
#define MYDATE_H
#include <string>
using namespace std;
class myDate
{
myDate();
myDate(int M, int D, int Y);
myDate(const myDate &D);
string dayName();
int getMonth();
int getDay();
int getYear();
int Greg2Julian(int M, int D, int Y);
void Julian2Greg(int JD, int& J, int& K, int& I);
void display();
void increaseDate(int N);
void decreaseDate(int N);
int daysBetween(myDate D);
int dayOfYear();
};
#endif
和完整的cpp:
myDate.cpp
#include "stdafx.h"
#include <iostream>
#include <string>
#include "myDate.h"
using namespace std;
int month = 0, day = 0, year = 0;
string months[12] = { "January", "February", "March", "April", "May", "June", "July", "September", "October", "November", "December" };
int dayCount[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
string weekdays[7] = { "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Monday" };
myDate::myDate()
{
month = 5, day = 11, year = 1959; // Default date
}
myDate::myDate(int M, int D, int Y)
{
int nM = M; int nD = D; int nY = Y;
int JD = Greg2Julian(M, D, Y);
Julian2Greg(JD, nM, nD, nY);
if (nM == M && nD == D && nY == Y)
month = M, day = D, year = Y;
else
month = 5, day = 11, year = 1959;
}
myDate::myDate(const myDate& D)
{
month = D.month;
day = D.day;
year = D.year;
}
int myDate::getMonth()
{
return month;
}
int myDate::getDay()
{
return day;
}
int myDate::getYear()
{
return year;
}
int myDate::Greg2Julian(int M, int D, int Y)
{
return D - 32075 + 1461 * (Y + 4800 + (M - 14) / 12) / 4 + 367 * (M - 2 - (M - 14) / 12 * 12) / 12 - 3 * ((Y + 4900 + (M - 14) / 12) / 100) / 4;
}
void myDate::Julian2Greg(int JD, int& J, int& K, int& I) //use reference variables
{
int L = JD + 68569;
int N = 4 * L / 146097;
L = L - (146097 * N + 3) / 4;
I = 4000 * (L + 1) / 1461001;
L = L - 1461 * I / 4 + 31;
J = 80 * L / 2447;
K = L - 2447 * J / 80;
L = J / 11;
J = J + 2 - 12 * L;
I = 100 * (N - 49) + I + L;
}
void myDate::display()
{
cout << months[month-1] << " " << day << ", " << year;
}
void myDate::increaseDate(int N)
{
Julian2Greg(Greg2Julian(month, day, year) + N, month, day, year);
}
void myDate::decreaseDate(int N)
{
Julian2Greg(Greg2Julian(month, day, year) - N, month, day, year);
}
int myDate::daysBetween(myDate D)
{
return Greg2Julian(D.getMonth(), D.getDay(), D.getYear()) - Greg2Julian(month, day, year);
}
int myDate::dayOfYear()
{
int sum = 0;
for (int i = 0; i < month; i++)
{
if (month > i + 1)
sum += dayCount[i];
else
sum += day;
}
return sum;
}
string myDate::dayName()
{
int dayNum = Greg2Julian(month, day, year) %7;
return weekdays[dayNum];
}
答案 0 :(得分:2)
由于变量是全局变量,myDate
类的 所有 实例(在某种程度上)将共享相同的变量。如果您为一个对象更改month
,则会为所有对象更改它。
这就是为什么你会遇到错误的原因D.month
,因为month
类中没有成员myDate
。
如果您希望这三个人成为班级的实际成员,那么您需要做类似
的事情class myDate
{
int month, day, year;
public:
// The rest of the class and its member functions...
};
然后从源文件中删除全局变量。
最后,我建议你从一开始就获得a couple of good books to read。