[问题] C++ class的实作问题(解决)

楼主: kiwistar (神汁手)   2015-12-18 06:38:42
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
code::blocks
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
问题(Question):
Error message: undefined reference to
code(已经重新写过):
Complex.h:
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
public:
Complex();//default constructor
Complex(double, double);//constructor
Complex& cadd(Complex&);
Complex& csubtract(Complex&);
Complex& cmultiply(Complex&);
static void print(Complex&);
private:
double real;
double imaginary;
};//end class the_complex
#endif // COMPLEX_H
Complex.cpp:
#include<iostream>
#include"Complex.h"
using namespace std;
the_complex::Complex()//default constructor
{
real = 0;
imagunary = 0;
}
the_complex::Complex(double a,double b)//constructor
{
real = a;
imaginary = b;
}
Complex& the_complex::cadd(const Complex& a)
{
(*this).real += a.real;
(*this).imaginary += a.imaginary;
return *this;
}
Complex& the_complex::csubtract(Complex& a)
{
(*this).real -= a.real;
(*this).imaginary -= a.imaginary;
return *this;
}
Complex& the_complex::cmultiply(Complex& a)
{
(*this).real = a.real * (*this).real - (*this).imaginary * a.imaginary;
(*this).imaginary = a.imaginary * (*this).real + a.real *
(*this).imaginary;
return *this;
}
void the_complex::print(Complex& a)
{
cout<<a.real<<'+'<<a.imaginary<<'i'<<endl;
}
main.cpp:
#include <iostream>
#include "Complex.h"
#include <string>
using namespace std;
int main()
{
double x1, y1, x2, y2;
char s;
Complex result;
x1 = x2 = y1 = y2 = 0;
cout<<"Please enter the real and imaginary number of the first
complex:"<<endl;
cin>>x1>>y1;
Complex e1(x1, y1);
cout<<"Please enter the real and imaginary number of the second
complex:"<<endl;
cin>>x2>>y2;
Complex e2(x2, y2);
cout<<"Please choose an operation: + or - or *"<<endl;
cin>>s;
if (s == '+')
result = e1.cadd(e2);
else if (s == '-')
result = e1.csubtract(e2);
else if (s == '*')
result = e1.cmultiply(e2);
else
{
cout<<"Error input!"<<endl;
return 0;
}
the_complex::print(result);
return 0;
}
已经解决了.......
我没有用Add files,而是直接新增档案
所以对软件来说我没有把他们放在同一个project底下......
感谢C大和m大回答!!!
作者: matita (pencil)   2015-12-18 07:15:00
header file里面函数没加return type 不知道是不是这原因然后程式码尽量还是贴文字档会比较好看...
楼主: kiwistar (神汁手)   2015-12-18 07:24:00
已经改了,谢谢!!然后问题也变了
作者: matita (pencil)   2015-12-18 07:51:00
the_complex改成Complex
楼主: kiwistar (神汁手)   2015-12-18 09:31:00
你是说左上角的那个选单吗?那个怎么改?
作者: Caesar08 (Caesar)   2015-12-18 11:09:00
先把新的code贴上来。照你的写法,print应该要是static
楼主: kiwistar (神汁手)   2015-12-18 12:26:00
这边已经是新的code了我有发现一个地方,不知道是不是问题所在就是我的function没有从object来呼叫,但我是定义在clas里面。改成把函数(add, multiply...)改为static可解决吗
作者: Caesar08 (Caesar)   2015-12-18 12:30:00
你的the_complex没有改成Complex除此之外,.h里面不要打using namespace std;仔细看了一下你的add、subtract,一种方法是改成static一种方法是operator overloading+friend另外,standard里面有<complex>,你确定要自己做一个?
作者: kwpn (ITSST)   2015-12-19 10:49:00
干嘛要一堆(*this),觉得很烦吗
作者: BlazarArc (Midnight Sun)   2015-12-19 12:10:00
this根本多余
作者: IKAFIRE (没有)   2015-12-19 12:31:00
说不定是从C#带过来的习惯
作者: uranusjr (←這人是超級笨蛋)   2015-12-19 19:04:00
要用 this 不是不行, 但为什么不用 -> 呢...

Links booklink

Contact Us: admin [ a t ] ucptt.com