首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请教c++高手一个关于继承中的重载有关问题用的是vc6.0

2012-02-20 
请问c++高手一个关于继承中的重载问题用的是vc6.0#include iostream#include stringusingnamespacestd

请问c++高手一个关于继承中的重载问题用的是vc6.0
#include <iostream>
#include <string>
using   namespace   std;
class   Point
{public:
Point(float   x   =   0,   float   y   =   0);
void   setPoint(float,   float);
float   getX()   const{return   x;}
float   gety()   const{return   y;}
friend   ostream   &operator   < <(ostream   &,   const   Point&);
protected:
float   x,   y;
};

Point::Point(float   a,   float   b)
{
x   =   a;
y   =   b;
}

void   Point::setPoint(float   a,   float   b)
{
x   =   a;
y   =   b;
}

ostream   &operator   < <(ostream   &output,const   Point   &p)
{

output < < "[ " < <p.x < < ", " < <p.y < < "] " < <endl;
return   output;
}

int   main()
{
Point   p(3.5,6.4);
cout < < "x   =   " < <p.getX() < < ",y   =   " < <p.gety() < <endl;
p.setPoint(8.5,6.8);
cout < <p < <endl;
return   0;
}

--------------------Configuration:   temp   -   Win32   Debug--------------------
Compiling...
temp.cpp
F:\编程\14\temp.cpp(30)   :   error   C2248:   'x '   :   cannot   access   protected   member   declared   in   class   'Point '
                F:\编程\14\temp.cpp(12)   :   see   declaration   of   'x '
F:\编程\14\temp.cpp(30)   :   error   C2248:   'y '   :   cannot   access   protected   member   declared   in   class   'Point '
                F:\编程\14\temp.cpp(12)   :   see   declaration   of   'y '
F:\编程\14\temp.cpp(36)   :   warning   C4305:   'argument '   :   truncation   from   'const   double '   to   'float '
F:\编程\14\temp.cpp(38)   :   warning   C4305:   'argument '   :   truncation   from   'const   double '   to   'float '
F:\编程\14\temp.cpp(39)   :   error   C2593:   'operator   < < '   is   ambiguous
Error   executing   cl.exe.

temp.exe   -   3   error(s),   2   warning(s)
是不是说的是《重载的参数不能调用基类的参数啊,可是我声明成了友元函数啊,找了好久就是没有找到问?

[解决办法]
把VC6.0扔了,这玩意太老了。另外,程序无错。
[解决办法]
#include <iostream>
#include <string>
using namespace std;
class Point
{public:
Point(float x = 0, float y = 0);
void setPoint(float, float);
float getX() const{return x;}
float gety() const{return y;}
friend ostream &operator < <(ostream &, const Point&);
protected:
double x, y;
};

Point::Point(float a, float b)
{
x = a;
y = b;
}

void Point::setPoint(float a, float b)
{
x = a;
y = b;
}

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


{

output < < "[ " < <p.x < < ", " < <p.y < < "] " < <endl;
return output;
}

int main()
{
Point p(3.5f,6.4f);//这里数字常量用float类型的
cout < < "x = " < <p.getX() < < ",y = " < <p.gety() < <endl;
p.setPoint(8.5,6.8);
cout < <p < <endl;
return 0;
}

运行结果为:
x = 3.5,y = 6.4
[8.5,6.8]


//建议最好把float都改为double,编译器默认浮点类型为double
[解决办法]
vs2005上通过了,为什么要一直抱着VC6。0不放?该换了
[解决办法]
还没用过vs2005````不知道和6.0有什么区别没
[解决办法]
把头部改那下下:
#include <iostream.h>
#include <string.h>
//using namespace std;


VC6.0编译通过,
输出:
x = 3.5,y = 6.4
[8.5,6.8]

Press any key to continue

热点排行