初学C ,编译同一个程序,对VC6.0和VS2005的迷惑。

时间:2008-06-05 12:58:14   来源:论坛整理  作者:  编辑:chinaitzhe
小弟初学C ,写了下面这个小程序:

#include <iostream>
#include <ostream>
using namespace std;
class Point
{
public:
friend ostream & operator < <(ostream &o, const Point &p);
Point():x(0),y(0){}
Point(int x, int y):x(x),y(y){}
~Point(){}

private:
int x;
int y;
};

ostream & operator < <(ostream &o, const Point &p)
{
cout < < "the point is x=" < < p.x < < " y = " < < p.y;
return o;
}
int main(int a, char **b)
{
Point p(10, 20);
cout < < p;
return 0;
}


在VC6.0中编译不能通过:
Compiling...
c.cpp
e:\myproject\class\c.cpp(19) : error C2248: 'x' : cannot access private member declared in class 'Point'
e:\myproject\class\c.cpp(13) : see declaration of 'x'
e:\myproject\class\c.cpp(19) : error C2248: 'y' : cannot access private member declared in class 'Point'
e:\myproject\class\c.cpp(14) : see declaration of 'y'
e:\myproject\class\c.cpp(25) : error C2593: 'operator < <' is ambiguous
Error executing cl.exe.

而在VS2005中却可以编译运行。

哪位知道的帮我解释一下,先谢过了。
网友回复:public:
friend ostream & operator < <(ostream &o, const Point &p);
把friend那一行的声明放到private:里试试


网友回复:又是重载操作符在VC 6.0中不能在类外定义的问题。。
你在类中定义就行了。

等下看taodm来怎么说你,呵呵。。
网友回复:楼上说的不错,这是应为vc6.0 不支持标准C 的写法造成的。
网友回复:vc6里面要放在类定义里面才能编译,这是vc6的bug,lz不必太在意,vc6对c 标准支持确实不够好

C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





#include <iostream> 

#include <ostream> 

using namespace std; 

class Point 

{ 

public: 

    //friend ostream & operator <<(ostream &o, const Point &p); 

    Point():x(0),y(0){} 

    Point(int x, int y):x(x),y(y){} 

    ~Point(){} 

    

    friend ostream & operator <<(ostream &o, const Point &p) 

    { 

        cout << "the point is x=" << p.x << " y = " << p.y << endl; 

        return o; 

    } 

private: 

    int x; 

    int y; 

}; 





int main(int a, char **b) 

{ 

    Point p(10, 20); 

    cout << p << endl; 

    return 0; 

}




网友回复:了了,让我折腾了一早上,哈哈。
网友回复:去掉using namespace std;
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



class Point

{

public:

    friend ostream & operator <<(ostream &o, const Point &p);

    Point():x(0),y(0){}

    Point(int x, int y):x(x),y(y){}

    ~Point(){}

private:

    int x;

    int y;

};



ostream & operator <<(ostream &o, const Point &p)

{

    cout<< "the point is x=" << p.x << " y = " << p.y;

    return o;

}

int main(int a, char **b)

{

    Point p(10, 20);

    cout << p;

    return 0;

}     


网友回复:楼上的说法?
关键字:初学,编译,同一个,程序,VC,

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面