| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 367 人关注过本帖
标题:谁能帮我看看啊?关于 运算符重载的问题
只看楼主 加入收藏
沙漠的行者
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-6-22
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
谁能帮我看看啊?关于 运算符重载的问题
#ifndef PH_H
#define PH_H

#include <iostream>
using std::istream;
using std::ostream;

#include <string>
using std::string;

class AM
{
    friend ostream& operator<<(ostream &,const AM &);
    friend istream& operator>>(istream &,AM &);
private:
    string a;
};
#endif
#include <iomanip>
using std::setw;
#include "ph.h"
ostream &operator<<(ostream &put,AM &l)
{
    put<<l.a<<endl;
    return put;
}
istream &operator>>(istream &get,AM &l)
{
    get.ignore();
    get>>setw(3)>>l.a;
    return get;
}
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include "ph.h"
int main()
{
    AM ha;
    cin>>ha;
    cout<<ha<<endl;
    return 0;
}
结果却是::\c++\microsoft visual studio\myprojects\dfgdf\df.cpp(21) : fatal error C1083: Cannot open include file: 'ph.h': No such file or directory
???
搜索更多相关主题的帖子: return friend include private 
2011-06-22 16:14
ToBeStronger
Rank: 4
等 级:业余侠客
帖 子:61
专家分:239
注 册:2011-4-8
收藏
得分:10 
楼主我帮你程序改了改,发现几个错误,put<<l.a<<endl;改成put<<l.a<<std::endl;
friend ostream& operator<<(ostream &,const AM &);
应该吧const去掉,应为赋值操作修给了string a,所以不能定义const;
ph.h代码
#ifndef PH_H
#define PH_H

#include <iostream>
using std::istream;
using std::ostream;

#include <string>
using std::string;

class AM
{
    friend ostream& operator<<(ostream &,AM &);
    friend istream& operator>>(istream &,AM &);
private:
    string a;
};
#endif

ph.cpp 代码
#include <iomanip>
using std::setw;
#include "ph.h"
ostream &operator<<(ostream &put,AM &l)
{
    put<<l.a<<std::endl;
    return put;
}
istream &operator>>(istream &get,AM &l)
{
    get.ignore();
    get>>setw(3)>>l.a;
    return get;
}

main函数代码

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include "ph.h"
int main()
{
    AM ha;
    cin>>ha;
    cout<<ha<<endl;
    return 0;
}
运行应该能成功
2011-06-23 12:52
jbd0513
Rank: 2
等 级:论坛游民
帖 子:7
专家分:20
注 册:2011-6-17
收藏
得分:10 
没看懂!
2011-06-24 16:48
ToBeStronger
Rank: 4
等 级:业余侠客
帖 子:61
专家分:239
注 册:2011-4-8
收藏
得分:0 
回复 3楼 jbd0513
其实是重载输入输出操作符........
2011-06-25 01:08
快速回复:谁能帮我看看啊?关于 运算符重载的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011635 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved