LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 593|回复: 0

请大家帮忙看看这个编译错误如何解决

[复制链接]
发表于 2006-6-2 14:34:08 | 显示全部楼层 |阅读模式
我尝试用Eclipse+CDT定义一个抽象数据类型,编译的时候总是在C01_introduction.cpp中第一个函数定义处报错:错误: expected initializer before ‘operator’。请问大家这是什么意思,我该如何消除这个错误呢?

代码如下:
下面是C01_introduction.h文件内容:
#ifndef C01_INTRODUCTION_H_
#define C01_INTRODUCTION_H_
#include <iostream>
using namespace std;

class Complex {                //complex number class
        double rp;                //real part
        double ip;                //imaginary part
public:
        Complex():rp(0),ip(0){}
        Complex(double r):rp(r),ip(0){}
        Complex(double r, double i):rp(r),ip(i){}
        double real() { return rp; }                                                                        //get current real part value
        double real(double r) { return rp = r; }                                        //change real part value
        double imaginary() { return ip; }                                        //get current imaginary part value
        double imaginary(double i) { return ip = i; }                //change imaginary part value
        friend const Complex operator+(const Complex& lv, const Complex& rv);
        friend const Complex operator-(const Complex& lv, const Complex& rv);
        friend const Complex operator*(const Complex& lv, const Complex& rv);
        friend const Complex operator/(const Complex& lv, const Complex& rv);
        friend ostream& operator<<(const ostream& os, const Complex& ob);
}          

#endif /*C01_INTRODUCTION_H_*/


下面是C01_introduction.cpp文件内容:
#include "C01_introduction.h"

        const Complex operator+(const Complex& lv, const Complex& rv) {
                return Complex(lv.rp + rv.rp, lv.ip + rv.ip);
        }
               
        const Complex operator-(const Complex& lv, const Complex& rv) {
                return Complex(lv.rp - rv.rp, lv.ip - rv.rp);
        }
       
        const Complex operator*(const Complex& lv, const Complex& rv) {
                return Complex(lv.rp * rv.rp - lv.ip * rv.ip, lv.rp * rv.ip + lv.ip * rv.rp);
        }
       
        const Complex operator/(const Complex& lv, const Complex& rv) {
                double d = rv.rp * rv.rp + rv.ip * rv.ip;
                return Complex((lv.rp * rv.rp - lv.ip * rv.ip)/d, (-lv.rp * rv.ip + lv.ip * rv.rp)/d);
        }
       
        ostream& operator<<(const ostream& os, const Complex& ob) {
                const char* sbl = (ob.ip >= 0.0)? " +":" ";
                return os << ob.rp << sbl << ob.ip << "i";
        }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表