问题:c++代码error,解决不了,求高手帮忙。。。
#include<iostream>
using namespace std;
class Complex
{
public:
Complex(float r1,float i1);
Complex(float r1);
void add(Complex &c);
void show();
private:
float real;
float imag;
}
Complex::Complex( float r1,float i1)
{
real=r1;
imag=i1;
}
void Complex::add(Complex &c)
{
real+=c.real;
imag+=c.imag;
}
void Complex::show()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
Complex::Complex(float r1)
{
real=r1;
imag=0;
}
int main()
{
Complex c1(3,5);
Complex c2=4.5;
c1.add(c2);
c1.show();
return 0;
}
c++实现复数运算。。。。求帮助。。
[解决办法]
class后面没有分号。
[解决办法]
test.h
#include<iostream>
class Complex
{
public:
Complex(float r1,float i1);
Complex(float r1);
void add(Complex &c);
void show();
private:
float real;
float imag;
};
Test.cpp
#include<iostream>
#include"test.h"
Complex::Complex( float r1,float i1){
real=r1;
imag=i1;
}
void Complex::add(Complex &c){
real+=c.real;
imag+=c.imag;
}
void Complex::show(){
std::cout<<real<<"+"<<imag<<"i"<<std::endl;
}
Complex::Complex(float r1){
real=r1;
imag=0;
}
int main()
{
Complex c1(3,5);
Complex c2=4.5;
c1.add(c2);
c1.show();
return 0;
}
这样试试
[解决办法]
class Complex
{
public:
Complex(float r1,float i1);
Complex(float r1);
void add(Complex &c);
void show();
private:
float real;
float imag;
};
后边少了个";"