C++输入、输出 运算符重载问题!
//person.h头文件#ifndefPERSON_H#definePERSON_H#include<string>#include<iostream>usingnam...
//person.h头文件
#ifndef PERSON_H
#define PERSON_H
#include<string>
#include<iostream>
using namespace std;
class person
{
string name;
int fenshu;
public:
friend istream & operator>>(istream &in,person & a);
friend ostream & operator<<(ostream & out, const person &a);
};
istream & operator>>(istream &in,person & a)
{
in>>a.name>>a.fenshu;
if(in)
cout<<"输入成功!"<<endl;
else
cout<<"输入失败!"<<endl;
return in;
}
ostream & operator<<(ostream & out, const person &a)
{
out<<a.name<<" "<<a.fenshu<<endl;
return out;
}
#endif
//main函数
#include "stdafx.h"
#include"person.h"
int _tmain(int argc, _TCHAR* argv[])
{
person aaa;
cin>>aaa;
cout<<aaa;
return 0;
}
这是在VS2010中的代码,总提示符号从定义错误;
1>person.obj : error LNK2005: "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class person &)" (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@AAVperson@@@Z) 已经在 operator_in_out.obj 中定义
1>person.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 person const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVperson@@@Z) 已经在 operator_in_out.obj 中定义
1>C:\C++Primerlianxi\exercise1_21\operator_in_out\Debug\operator_in_out.exe : fatal error LNK1169: 找到一个或多个多重定义的符号
请问如何解决输入、输出符号的重定义问题,我的代码错在哪里? 展开
#ifndef PERSON_H
#define PERSON_H
#include<string>
#include<iostream>
using namespace std;
class person
{
string name;
int fenshu;
public:
friend istream & operator>>(istream &in,person & a);
friend ostream & operator<<(ostream & out, const person &a);
};
istream & operator>>(istream &in,person & a)
{
in>>a.name>>a.fenshu;
if(in)
cout<<"输入成功!"<<endl;
else
cout<<"输入失败!"<<endl;
return in;
}
ostream & operator<<(ostream & out, const person &a)
{
out<<a.name<<" "<<a.fenshu<<endl;
return out;
}
#endif
//main函数
#include "stdafx.h"
#include"person.h"
int _tmain(int argc, _TCHAR* argv[])
{
person aaa;
cin>>aaa;
cout<<aaa;
return 0;
}
这是在VS2010中的代码,总提示符号从定义错误;
1>person.obj : error LNK2005: "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class person &)" (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@AAVperson@@@Z) 已经在 operator_in_out.obj 中定义
1>person.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 person const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVperson@@@Z) 已经在 operator_in_out.obj 中定义
1>C:\C++Primerlianxi\exercise1_21\operator_in_out\Debug\operator_in_out.exe : fatal error LNK1169: 找到一个或多个多重定义的符号
请问如何解决输入、输出符号的重定义问题,我的代码错在哪里? 展开
2个回答
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询