C++的类份文件编写编译错误,想不通错在哪
主函数:
#include
#include "Time.h"
#include "Date.h"
using namespace std;
void display(Date& d,Time &t)
{
cout << d.month << "/" << d.day << "/" << d.year << endl;
cout << t.hour << ":" << t.minute << ":" << t.sec << endl;
}
int main()
{
Time t1(10, 13, 56);
Date d1(12, 25, 2004);
display(d1, t1);
system("pause");
return 0;
}
第一个分文件:
#pragma once
#include
#include "Date.h"
using namespace std;
//class Date
class Time
{
public:
Time(int, int, int);
friend void display(Date& d, Time& t);
private:
int hour;
int minute;
int sec;
};
Time::Time(int h, int m, int s)
{
hour = h; minute = m; sec = s;
}
第二个分文件:
#pragma once
#include
#include "Time.h"
using namespace std;
//class Time;
class Date
{
public:
Date(int, int, int);
friend void display(Date& d, Time& t);
private:
int month;
int day;
int year;
};
Date::Date(int m, int d, int y)
{
month = m; day = d; year = y;
}
类的定义和声明是两种东西,如果你类里面的函数定义为static,则可以把该类当作命名空间使用,否则就算定义了也得声明才能够对该对象进行操作
可以参考:
https://download.csdn.net/download/Dana_ranmo/85055723