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

操作符 << 满载 出现报错

2014-01-22 
操作符 重载出现报错在网上找了找关于操作符重载的文章,自己折腾了半天,遇到重载时就出错了不知道为

操作符 << 重载 出现报错
在网上找了找关于操作符重载的文章,自己折腾了半天,遇到<<重载时就出错了不知道为什么?
源程序如下:(自己建的类Jase)


#ifndef JASECLASS_H
#define JASECLASS_H
#include<iostream>
using std::cout;
using std::endl;

class Jase;
ostream& operator<<(ostream& out,Jase& a);

class Jase
{
public:
    Jase(double _data = 0.0):data(_data){}
    Jase& operator = (const Jase& rhs)
    {
        data = rhs.data;
        return *this;
    }
friend ostream& operator << (ostream& out,Jase& a);
private:
    double data;
};

ostream& operator << (ostream& out,Jase& a)
{
    out << a.data ;
    return out;
}
#endif

报出的错误如下:

--------------------Configuration: Demo - Win32 Debug--------------------
Compiling...
Demo.cpp
i:\demo\jaseclass.h(8) : error C2143: syntax error : missing ';' before '&'
i:\demo\jaseclass.h(8) : error C2501: 'ostream' : missing storage-class or type specifiers
i:\demo\jaseclass.h(8) : error C2061: syntax error : identifier 'ostream'
i:\demo\jaseclass.h(8) : error C2501: '<<' : missing storage-class or type specifiers
i:\demo\jaseclass.h(8) : error C2809: 'operator <<' has no formal parameters
i:\demo\jaseclass.h(19) : error C2143: syntax error : missing ';' before '&'
i:\demo\jaseclass.h(19) : error C2433: 'ostream' : 'friend' not permitted on data declarations
i:\demo\jaseclass.h(19) : error C2501: 'ostream' : missing storage-class or type specifiers
i:\demo\jaseclass.h(19) : error C2244: 'ostream' : unable to resolve function overload
i:\demo\jaseclass.h(19) : error C2061: syntax error : identifier 'ostream'
i:\demo\jaseclass.h(19) : error C2501: '<<' : missing storage-class or type specifiers
i:\demo\jaseclass.h(19) : error C2805: binary 'operator <<' has too few parameters
i:\demo\jaseclass.h(24) : error C2143: syntax error : missing ';' before '&'
i:\demo\jaseclass.h(24) : error C2501: 'ostream' : missing storage-class or type specifiers
i:\demo\jaseclass.h(24) : error C2086: 'ostream' : redefinition
i:\demo\jaseclass.h(24) : error C2061: syntax error : identifier 'ostream'
i:\demo\jaseclass.h(25) : error C2501: '<<' : missing storage-class or type specifiers
i:\demo\jaseclass.h(25) : error C2809: 'operator <<' has no formal parameters
i:\demo\jaseclass.h(26) : error C2065: 'out' : undeclared identifier
i:\demo\jaseclass.h(26) : error C2065: 'a' : undeclared identifier
i:\demo\jaseclass.h(26) : error C2228: left of '.data' must have class/struct/union type
执行 cl.exe 时出错.
Demo.exe - 1 error(s), 0 warning(s)


我自己查了查没什么进展?报错指出的是
ostream& operator<<(ostream& out,Jase& a);

这行代码是一个全局函数的声明;就这个函数的写法就挺奇特,(对于我来说。)

这样的函数声明第一次见,想试试吧还出了Bug,无从着手调试啊!!请帮帮忙----
[解决办法]
using std::ostream;

热点排行