流重载

magichuang 2008-03-20 12:53:33
我现在想用流重载输出CDate类,如果我把实现放在Date.cpp或main函数文件里,就没有问题
但是如果我把实现放在Date.h文件下就会出问题。

如果我这样写:
#include <iostream>
using namespace std;


#if !defined(AFX_DATE_H__91F06C7A_00D0_410C_86AE_0C76D3E71326__INCLUDED_)
#define AFX_DATE_H__91F06C7A_00D0_410C_86AE_0C76D3E71326__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CDate
{
friend ostream& operator<<( ostream&, const CDate& );
public:
CDate(int = 2008, int = 1 ,int = 1);
CDate(const CDate &);
void setDate(int, int, int);
void setYear(int );
void setMonth( int );
void setDay ( int );
int getYear() const;
int getMonth() const;
int getDay() const;
bool isLeapYear( int ) const;
void nextDay();
virtual ~CDate();
private:
int year, month, day;
int checkDays( int, int , int);

};
ostream& operator<< ( ostream& output, const CDate& d)
{
output<< "Year: " << d.getYear() << " Month: " << d.getMonth() << " Day: " << d.getDay() << endl;
return output;
}


提示错误 :这一行 cout << birthday;错误error C2593: 'operator <<' is ambiguous


如果我单写using std::ostream;
using std::cout;
using std::endl;
也在.h文件中,错误:
Student.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class CDate const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@
std@@AAV01@ABVCDate@@@Z) already defined in Date.obj
Test_CStudent_main.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class CDate const &)" (??6@YAAAV?$basic_ostream@DU?$char_trai
ts@D@std@@@std@@AAV01@ABVCDate@@@Z) already defined in Date.obj
Debug/Test_CStudent.exe : fatal error LNK1169: one or more multiply defined symbols found

麻烦高手给解释一下这两种错误是什么原因还有为什么会造成这两种错误。
...全文
99 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wpalhm 2008-03-20
  • 打赏
  • 举报
回复
6楼正解
ZiSheng 2008-03-20
  • 打赏
  • 举报
回复
楼上的说的对极了
jixingzhong 2008-03-20
  • 打赏
  • 举报
回复
非inline的放在h文件中,该头文件又没有设法防止 重复包含
那么该定义在工程编译中会出现多次,
结果就是 重复定义,
出现的错误类似:....already defined in *.obj
abupie 2008-03-20
  • 打赏
  • 举报
回复
实现不放在.h的好处是 有修改不需要全部重编译.仅仅需要重连接。
如2楼所说:“要放到.h里的话一般只有 1)类的成员函数声明和定义放一起 2) inline函数”,这个应该是编码的一个基本原则。
ryfdizuo 2008-03-20
  • 打赏
  • 举报
回复
一般重载流的函数直接写在类内部就可以的,代码也不多的,
星羽 2008-03-20
  • 打赏
  • 举报
回复
要放到.h里的话一般只有
1)类的成员函数声明和定义放一起,比如


struct test {
void fun() {
}
};

2) inline函数


星羽 2008-03-20
  • 打赏
  • 举报
回复

inline ostream& operator<< ( ostream& output, const CDate& d)
{
output<< "Year: " << d.getYear() << " Month: " << d.getMonth() << " Day: " << d.getDay() << endl;
return output;
}

ps:建议不要写在.h
abupie 2008-03-20
  • 打赏
  • 举报
回复
编译没有先clean干净导致的吧。
先清掉所有的obj,再重编译。

另外,不建议实现放在.h中。

64,849

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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