// Lab 4: Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
/* Write class definition for Complex */
class Complex {
public:
Complex(double, double);
Complex();
void add(Complex &right);
void subtract(Complex &right);
void setComplexNumber(double,double);
void printComplex();
private:
double imaginaryPart;
double realPart;
};
#endif
.h文件是这样
// Lab 4: Complex.cpp
// Member-function definitions for class Complex.
#include <iostream>
using namespace std;
#include "Complex.h"
Complex::Complex( double real, double imaginary )
{
setComplexNumber( real, imaginary );
} // end Complex constructor
Complex Complex::add( Complex &right )
{
Complex num;
num.imaginaryPart = imaginaryPart + right.imaginaryPart;
num.realPart = realPart + right .realPart;
return num;
/* Write a statement to return a Complex object. Add
the realPart of right to the realPart of this Complex
object and add the imaginaryPart of right to the
imaginaryPart of this Complex object */
} // end function add
Complex Complex ::subtract(Complex& right)//然后这个地方报错
//声明与 "void Complex::add(Complex &right)" (已声明 所在行数:10,所属文件:"C:\USERS\86182\DESKTOP\Complex.h") 不兼容 Project19 C:\Users\86182\Desktop\Complex.cpp 12
{
Complex num;
num.imaginaryPart = imaginaryPart - right.imaginaryPart;
num.realPart = realPart - right.realPart;
return num;
/* Write a statement to return a Complex object. Subtract
the realPart of right from the realPart of this Complex
object and subtract the imaginaryPart of right from
the imaginaryPart of this Complex object */
} // end function subtract
void Complex::printComplex()
{
cout << '(' << realPart << ", " << imaginaryPart << ')';
} // end function printComplex
void Complex::setComplexNumber( double rp, double ip )
{
realPart = rp;
imaginaryPart = ip;
} // end function setComplexNumber
你的声明是void 实现却有返回值。
返回值不对一个是void,一个是complex