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

实现负数运算的类,该怎么处理

2012-02-29 
实现负数运算的类//complex0.h#include iostream#ifndefCOMPLEX0_H_#defineCOMPLEX0_H_classComplex{pri

实现负数运算的类
//complex0.h
#include <iostream>
#ifndef   COMPLEX0_H_
#define   COMPLEX0_H_
class   Complex
{
private:
double   real;
double   image;
public:
Complex();
Complex(double   dreal,double   dimage=0.0);
~Complex();
friend   Complex   operator+(const   Complex   &a,const   Complex   &b);
friend   Complex   operator-(const   Complex   &a,const   Complex   &b);
friend   Complex   operator*(const   Complex   &a,const   Complex   &b);
friend   Complex   operator~(const   Complex   &a);
friend   Complex   operator*(const   Complex   &a,double   n);
friend   Complex   operator*(double   n,const   Complex   &a);
friend   std::ostream   &   operator < <(std::ostream   &os,const   Complex&a);
friend   std::istream   &   operator> > (std::istream   &os,const   Complex&a);
}
#endif   COMPLEX0_H_


//complex0.cpp
#include <iostream>
#include   "complex0.h "

Complex::Complex()
{
real=0.0;
image=0.0;
}
Complex::Complex(double   dreal,double   dimage)
{
real=dreal;
image=dimage;
}
Complex::~Complex(){}
Complex   operator+(const   Complex   &a,const   Complex   &b)
{
return   Complex(a.real+b.real,a.image+b.image);
}
Complex   operator-(const   Complex   &a,const   Complex   &b)
{
return   Complex(a.real-b.real,a.image-b.image);
}
Complex   operator*(const   Complex   &a,const   Complex   &b)
{
return   Complex(a.real*b.real-a.image*b.image,a.real*b.image+a.image*b.real);
}
Complex   operator*(const   Complex   &a,double   n)
{
return   Complex(a.real*n,a.image*n);
}
Complex   operator*(double   n,const   Complex   &a)
{
return   a*n;
}
Complex   operator~(const   Complex   &a)
{
return   Complex(a.real,-a.image);
}
std::ostream   &   operator < <(std::ostream   &os,const   Complex&a)
{
os < < "( " < <a.real < < ", " < <a.image < < "i) ";
return   os;
}
std::istream   &   operator> > (std::istream   &is,const   Complex&a)
{
std::cout < < "real: ";
is> > a.real;
std::cout < < "imaginary: ";
is> > a.image;
return   is;
}
//usecomplex0.cpp
#include <iostream>
using   namespace   std;
#include   "complex0.h "  
int   main()
{
Complex   a(3.0,4.0);
Complex   c;
cout < < "Enter   a   complex   number(q   to   quit) " < <endl;
while(cin> > c)
{
cout < < "c   is   " < <c < <endl;
cout < < "complex   conjugate   is   " < <~c < <endl;
cout < < "a   is   " < <a < <endl;
cout < < "a+c   is   " < <a+c < <endl;
cout < < "a-c   is   " < <a-c < <endl;


cout < < "a*c   is   " < <a*c < <endl;
cout < < "2*a   is   " < <2*a < <endl;
cout < < "Enter   a   complex   number(q   to   quit) " < <endl;
}
cout < < "Done! " < <endl;
system( "pause ");
return   0;
}


编译的时候会出现  
c:\documents   and   settings\新\my   documents\visual   studio   2005\projects\zmx\zmx\complex0.cpp(5)   :   error   C2533:   'Complex::{ctor} '   :   constructors   not   allowed   a   return   type
usecomplex0.cpp
c:\documents   and   settings\新\my   documents\visual   studio   2005\projects\zmx\zmx\usecomplex0.cpp(4)   :   error   C2628:   'Complex '   followed   by   'int '   is   illegal   (did   you   forget   a   '; '?)
c:\documents   and   settings\新\my   documents\visual   studio   2005\projects\zmx\zmx\usecomplex0.cpp(5)   :   error   C3874:   return   type   of   'main '   should   be   'int '   instead   of   'Complex '
这样的三个错误。。。
如果我将实现中的两个构造函数的顺序对调,除了那三个错误之外还会出现很多
error   C2264:   'Complex::Complex '   :   error   in   function   definition   or   declaration;   function   not   called   这样的错误

哪儿错了?还有哪儿有待修改?

[解决办法]
//complex0.h
#include <iostream>
#ifndef COMPLEX0_H_
#define COMPLEX0_H_
class Complex
{
private:
double real;
double image;
public:
Complex();
Complex(double dreal,double dimage=0.0);
~Complex();
friend Complex operator+(const Complex &a,const Complex &b);
friend Complex operator-(const Complex &a,const Complex &b);
friend Complex operator*(const Complex &a,const Complex &b);
friend Complex operator~(const Complex &a);
friend Complex operator*(const Complex &a,double n);
friend Complex operator*(double n,const Complex &a);
friend std::ostream & operator < <(std::ostream &os,const Complex&a);
friend std::istream & operator> > (std::istream &os,const Complex&a);
}; //!!这里加个分号
#endif COMPLEX0_H_

热点排行