请问expected initializer before ‘operator’是什么意思?
我尝试在Ubuntu下用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";
}