运算符重载
总是提示我找不到标识符,未声明的标识符,新建了一个Complex类
这是Complex.h
#pragma once
#include<iostream>
using namespace std;
class Complex
{
public:
Complex(){real=0,imag=0;}
Complex(double r,double i)
{
real=r,imag=i;
}
friend Complex operator+(Complex &c1,Complex &c2);
friend Complex operator-(Complex &c1,Complex &c2);
friend void print(Complex &c);
private:
double real,imag;
~Complex(void);
};
这是Complex.cpp
#include "StdAfx.h"
#include "Complex.h"
#include <iostream>
using namespace std;
Complex::~Complex(void)
{
}
Complex operator+(Complex &c1,Complex &c2)
{
return Complex(c1.real+c2.real,c1.imag+c2.imag);
}
Complex operator-(Complex &c1,Complex &c2)
{
return Complex(c1.real-c2.real,c1.imag-c2.imag);
}
void print(Complex &c)
{
if(c.imag<0)
cout<<c.real<<c.imag<<'i';
else
cout<<c.real<<'+'<<c.imag<<'i';
}
#include "stdafx.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Complex c1(2.0,3.0),c2(4.0,-1.0),c3;
c3=c1+c2;
cout<<"/nc1+c2=";
print(c3);
c3=c1-c2;
cout<<"/nc1-c2=";
print(c3);
cout<<endl;
return 0;
}
求各位大佬帮帮忙,看看哪里错了,求轻喷