| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1081 人关注过本帖
标题:这段代码运行不了是什么原因呢?
只看楼主 加入收藏
bc574479449
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-10-18
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:15 
这段代码运行不了是什么原因呢?
#include <iostream>
using namespace std;
class point
{int x,y;
public:point(int x1=0,int y1=0):x(x1),y(y1){}
       friend ostream& operator<<(ostream&, const point&);
       friend istream& operator>>(istream&,point&);
};
ostream& operator<<(ostream& out, const point& src)
{ out<<'<'<<src.x<<','<<src.y<<'>';
return out;
}
istream& operator>>(istream& in,point& target)
{ in>>target.x>>target.y;
return in;
}
int main()
{point a(1,2);
cin>>a;
cout<<a;
return 0;
}
搜索更多相关主题的帖子: 代码 运行 
2010-10-31 23:12
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:11 
#include <iostream.h>

class point
{
    int x,y;
public:
    point(int x1=0,int y1=0):x(x1),y(y1){}      
    friend ostream& operator<<(ostream&, const point&);     
    friend istream& operator>>(istream&, point&);
};
ostream& operator<<(ostream& out, const point& src)
{
    out << '<' << src.x << ',' << src.y << '>' ;
    return out;
}

istream& operator>>(istream& in,point& target)
{
    in>> target.x >> target.y;
    return in;
}
int main()
{
    point a(1,2);
    cin>>a;
    cout<<a;
    return 0;
}

好像大部分 都是如此
 
2010-11-01 06:59
bc574479449
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-10-18
收藏
得分:0 
你能帮解释下为什么这样修改吗?#include <iostream.h>的作用是什么呢?谢谢了!
2010-11-01 09:41
missiyou
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:531
专家分:218
注 册:2007-10-9
收藏
得分:7 
#include<iostream>
using namespace std;
class point
{
    public:
        point(int px = 0, int py = 0):x(px), y(py){ //pass
        }
        friend ostream& operator << (ostream&, const point&);
        friend istream& operator >> (ostream&, point&);
    // private:
    public:
        int x;
        int y;
};

ostream& operator << (ostream& out, const point& src)
{
    out<< "< " <<src.x<< " , "<<src.y<< " >";
    return out;
}

istream& operator >>(istream& in, point& target)
{
    in >> target.x >>target.y;
    return in;
}
int main(int argc, char* argv[])
{
    int x, y;
    x = y = 3;
    point P(x, y);
    cin >> P; //输入: 3 8 (形式)
    cout << P;

}
2010-11-01 10:08
missiyou
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:531
专家分:218
注 册:2007-10-9
收藏
得分:0 
int x,y; 默认是私有,
改成公共的,把(int x,y;) 放在 Public:下面应该就可以了.
2010-11-01 10:11
bc574479449
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-10-18
收藏
得分:0 
我试过了,编译不过!你上面修改过的代码也运行不了!!
2010-11-01 11:07
bc574479449
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-10-18
收藏
得分:0 
类的“友员函数”不是可以访问类的私有成员吗!?
2010-11-01 11:14
yangang2
Rank: 6Rank: 6
等 级:侠之大者
威 望:1
帖 子:64
专家分:432
注 册:2010-9-1
收藏
得分:0 
你的#include <iostream>改为#include <iostream.h>,把using namespace std;删除就能运行了,友元函数本来就能访问私有的数据成员,不必改为公有的。你是由于上面的错误编译器误报出不能访问私有成员。
2010-11-01 12:10
bc574479449
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-10-18
收藏
得分:0 
那什么时候用“#include<iostream> using namespace std;”?而“#include <iostream.h>”又是在什么情况下用呢?懂的帮解释下!非常感谢了!
2010-11-01 13:03
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:2 
“#include<iostream> using namespace std;”这是C++语言里的!!!“#include <iostream.h>”这是C语言

If You Want Something, Go Get It, Period.
2010-11-01 13:10
快速回复:这段代码运行不了是什么原因呢?
数据加载中...
 
   



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

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