[問題] C++ class的實作問題(解決)
開發平台(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大回答!!!
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.239.250.138
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1450391926.A.013.html
※ 編輯: kiwistar (36.239.250.138), 12/18/2015 07:21:15
推
12/18 07:15, , 1F
12/18 07:15, 1F
→
12/18 07:17, , 2F
12/18 07:17, 2F
※ 編輯: kiwistar (36.239.250.138), 12/18/2015 07:24:08
→
12/18 07:24, , 3F
12/18 07:24, 3F
→
12/18 07:51, , 4F
12/18 07:51, 4F
→
12/18 09:31, , 5F
12/18 09:31, 5F
→
12/18 11:09, , 6F
12/18 11:09, 6F
→
12/18 12:26, , 7F
12/18 12:26, 7F
→
12/18 12:27, , 8F
12/18 12:27, 8F
→
12/18 12:27, , 9F
12/18 12:27, 9F
→
12/18 12:28, , 10F
12/18 12:28, 10F
→
12/18 12:30, , 11F
12/18 12:30, 11F
他不讓我改...還是有其他地方可以設定?
左上角那個框框
→
12/18 12:31, , 12F
12/18 12:31, 12F
→
12/18 12:33, , 13F
12/18 12:33, 13F
→
12/18 12:34, , 14F
12/18 12:34, 14F
我也想過用operator overloding的方法
可是這個題目出現在class的章節
我猜他應該認為這用member function做就可
改static可以解決ISO C++ forbids declaration of XXX with no type這個error嗎?
→
12/18 12:38, , 15F
12/18 12:38, 15F
這是課後練習的作業TwT
※ 編輯: kiwistar (223.142.239.56), 12/18/2015 12:54:44
※ 編輯: kiwistar (223.142.239.56), 12/18/2015 13:53:42
※ 編輯: kiwistar (223.142.239.56), 12/18/2015 13:55:29
※ 編輯: kiwistar (223.142.239.56), 12/18/2015 13:56:54
※ 編輯: kiwistar (223.142.239.56), 12/18/2015 14:39:55
推
12/19 10:49, , 16F
12/19 10:49, 16F
推
12/19 12:10, , 17F
12/19 12:10, 17F
推
12/19 12:31, , 18F
12/19 12:31, 18F
→
12/19 19:04, , 19F
12/19 19:04, 19F