我定义了两个类,设置其中一个为另一个的友元类,可是编译报错其中一个类的构造函数重复定义
DATE.h:
#include
using namespace std;
class Time;
class Date {
private:
int month;
int day;
int year;
public:
Date(int, int, int); //构造函数
friend Time; //Time为友元类
};
Date::Date(int m, int d, int y) {
month = m;
day = d;
year = y;
}
TIME.h:
#include
class Date;
using namespace std;
class Time { //声明Time类
private:
int hour;
int minute;
int sec;
public:
Time(int, int, int);
void display(Date&);
};
Time实现:
#include "stdafx.h"
#include"TIME.h"
#include"DATE.h"
Time::Time(int h, int m, int s) {
hour = h;
minute = m;
sec = s;
}
void Time::display(Date &d) {
cout << d.month << "/" << d.day << "/" << d.year << endl;
cout << hour << ":" << minute << ":" << sec << endl;
}
报错:
LNK2005 "public: __thiscall Date::Date(int,int,int)" (??0Date@@QAE@HHH@Z) 已经在 functions(Time).obj 中定义
第二个函数不写构造函数,直接用