注册 登录
编程论坛 C++教室

这段代码运行不了是什么原因呢?

bc574479449 发布于 2010-10-31 23:12, 1081 次点击
#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;
}
15 回复
#2
寒风中的细雨2010-11-01 06:59
#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;
}

好像大部分 都是如此
 
#3
bc5744794492010-11-01 09:41
你能帮解释下为什么这样修改吗?#include <iostream.h>的作用是什么呢?谢谢了!
#4
missiyou2010-11-01 10:08
#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;

}
#5
missiyou2010-11-01 10:11
int x,y; 默认是私有,
改成公共的,把(int x,y;) 放在 Public:下面应该就可以了.
#6
bc5744794492010-11-01 11:07
我试过了,编译不过!你上面修改过的代码也运行不了!!
#7
bc5744794492010-11-01 11:14
类的“友员函数”不是可以访问类的私有成员吗!?
#8
yangang22010-11-01 12:10
你的#include <iostream>改为#include <iostream.h>,把using namespace std;删除就能运行了,友元函数本来就能访问私有的数据成员,不必改为公有的。你是由于上面的错误编译器误报出不能访问私有成员。
#9
bc5744794492010-11-01 13:03
那什么时候用“#include<iostream> using namespace std;”?而“#include <iostream.h>”又是在什么情况下用呢?懂的帮解释下!非常感谢了!
#10
m21wo2010-11-01 13:10
“#include<iostream> using namespace std;”这是C++语言里的!!!“#include <iostream.h>”这是C语言
#11
寒风中的细雨2010-11-01 16:50
一句话 bug
#12
huazhao2010-11-01 23:10
现在的VC++ .h加不加是一样的啦
#13
Only_Boby2010-11-03 10:37
头文件改成#include <iostream.h>   应该就可以了
#14
Only_Boby2010-11-03 10:41
回复 2楼 寒风中的细雨
头文件  c里的  找本简单的介绍c的书就会有的  执行头文件必用的  可以说是比较通用的一种命名
#15
Only_Boby2010-11-03 12:49
#include <iostream.h>  自认为是一种习惯  少了就会减少很多没必要的麻烦
#16
ytfsksk2010-11-03 17:23
#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)
{ cout<<'<'<<src.x<<','<<src.y<<'>';
return out;
}
istream& operator>>(istream& in,point& target)
{ cin>>target.x>>target.y;
return in;
}
int main()
{point a(1,2);
cout<<"请输入:"<<endl;
cin>>a;
cout<<a;
return 0;
}
out和in编译器认识吗?cout,cin
1