多个头文件怎样确定包含关系

qazwsx123asdfadf 2010-08-17 04:54:12
Date.h
class Date
{
public:
Date(int year=0,int month=0,int day=0);
~Date(){cout<<"destructing Date"<<endl;}
void PrintDate();
int year;
int month;
int day;
};
CPerson.h
class CPerson
{
public:
CPerson(){}
CPerson(Date birthday,int Num,string sName="张三");
CPerson(int year,int month,int day,int Num,string sName="张三");
string GetName();
int GetNum();
Date GetBirthday();
friend bool Compare(CPerson &p1, CPerson &p2);
private:
Date birthday;
const string sName;
int Num;
};
CStudent.h
class CStudent:virtual private CPerson
{
public:
CStudent(){}
CStudent(CPerson person, int StuNum);
CStudent(Date birthday,int Num,int StuNum,string sName="张三");
CStudent(int year,int month,int day,int Num,int StuNum,string sName="张三");
string GetName();
Date GetBirthday();
int GetNum();
int GetStuNum();
private:
int StuNum;
};
CTeacher.h
class CTeacher:virtual public CPerson
{
public:
CTeacher(){}
CTeacher(CPerson person, Date time);
CTeacher(Date birthday,int Num,Date time,string sName="张三");
CTeacher(int year,int month,int day,int Num,Date time,string sName="张三");
Date GetTime() ;
private:
Date time;
};
CTA.h
class CTA:public CStudent,public CTeacher
{
public:
CTA(int year,int month,int day,int Num,int StuNum,Date time,int No,string sName="张三");
int GetNo();
private:
int No;
};
main.cpp
#include "CTA.h"
void main()
{
CPerson a(1,2,3,4,"asd");
}
错误为Compiling...
main.cpp
Linking...
CStudent.obj : error LNK2005: "public: __thiscall Date::Date(int,int,int)" (??0Date@@QAE@HHH@Z) already defined in CPerson.obj
CStudent.obj : error LNK2005: "public: void __thiscall Date::PrintDate(void)" (?PrintDate@Date@@QAEXXZ) already defined in CPerson.obj
CTA.OBJ : error LNK2005: "public: __thiscall Date::Date(int,int,int)" (??0Date@@QAE@HHH@Z) already defined in CPerson.obj
CTA.OBJ : error LNK2005: "public: void __thiscall Date::PrintDate(void)" (?PrintDate@Date@@QAEXXZ) already defined in CPerson.obj
CTeacher.obj : error LNK2005: "public: __thiscall Date::Date(int,int,int)" (??0Date@@QAE@HHH@Z) already defined in CPerson.obj
CTeacher.obj : error LNK2005: "public: void __thiscall Date::PrintDate(void)" (?PrintDate@Date@@QAEXXZ) already defined in CPerson.obj
main.obj : error LNK2005: "public: __thiscall Date::Date(int,int,int)" (??0Date@@QAE@HHH@Z) already defined in CPerson.obj
main.obj : error LNK2005: "public: void __thiscall Date::PrintDate(void)" (?PrintDate@Date@@QAEXXZ) already defined in CPerson.obj
Debug/CPerson.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

CPerson.exe - 9 error(s), 0 warning(s)
这是什么错误,这几个头文件应该怎样确定包含关系啊!(与他们相对应的cpp文件,我包含的相应的头文件)
谢谢大家了!!!
...全文
63 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qazwsx123asdfadf 2010-08-17
  • 打赏
  • 举报
回复
刚才找到错误了,Date的定义和说明我都放在Date.h里面了,把他们拆开后就没问题了。
都怪我没说清,谢谢大家了!
「已注销」 2010-08-17
  • 打赏
  • 举报
回复
在头前加
#pragma once
dingshaofengbinbin 2010-08-17
  • 打赏
  • 举报
回复
可能是A里面包含了B然后B里面又包含了A从而导致出错

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧