2011-01-23 15 views
-2
#include<iostream> 
#include<stdlib.h> 
#include"header_complex.h" 

using namespace std; 




class Complex 
{ 
private: 
float real,imag; 
public: 
Complex(); //default cnstructor 
Complex(float, float); //parametrize constructor 
void set_real(float); 
void set_imag(float); 
float get_real(); 
float get_imag(); 
void input(); 
void display(); 

}; 


int main() 
{ 
Complex c1; // object creation 

c1.display(); 
c1.input(); 
c1.display(); 

return EXIT_SUCCESS; 
} 


//Functions definations 

Complex::Complex(float a=0, float b=0) 
{ 
    real = a; 
    imag = b; 
} 

void Complex::set_real(float a) 
{ 
    a = real; 
} 

void Complex::set_imag(float a) 
{ 
    a = imag; 
} 

float Complex::get_real() 
{ 
    return real; 
} 
float Complex::get_imag() 
{ 
    return imag; 
} 

void Complex::input(){ 
    cout << "Enter Real part "; 
    cin >> real; 
    cout << "Enter Imaginary part " ; 
    cin >> imag; 
} 

void Complex::display() 
{ 
    cout << "Real part is " << real; 
    cout << "Imaginary part is " << imag; 
} 
+0

請格式化您的代碼,謝謝。 – ThomasMcLeod 2011-01-23 18:34:05

+0

請使用{}按鈕格式化您的源代碼。 – 2011-01-23 18:34:05

+4

好的,但是問題是什麼?任何錯誤?什麼不起作用 - 不要害怕包括細節。 – nos 2011-01-23 18:34:20

回答

3

錯誤LNK2019:MS你使用,同時它缺少的默認構造函數解析的外部 符號 「市民:__thiscall 複雜::複合物(無效)」 (?? 0Complex @@ QAE @ XZ) 功能_main

閱讀並理解錯誤信息是學習成功的一項非常重要的技能。 「未解析的外部」是鏈接器在看到您使用標識符時產生的錯誤消息,但在任何提供的.obj和.lib文件中找不到它的定義。

Complex :: Complex(void)是缺少的標識符。它是Complex類的構造函數,它沒有任何參數。你宣佈它,你使用它,你只是沒有寫它的代碼。

您可以通過在錯誤列表窗口中選擇錯誤消息並按F1來獲得有關鏈接器或編譯器錯誤的幫助。

0

你不能在函數定義提供默認參數;你必須在函數聲明中做到這一點。相反,聲明瞭兩個構造函數,聲明一個像

Complex(float a=0, float b=0); 
在你的定義

然後,刪除默認值,所以它成爲

Complex::Complex(float a, float b) 
1

我不明白爲什麼有人會如此含糊描述的錯誤信息(說,作爲一個「鏈接器錯誤」),而不復制和粘貼錯誤消息。也就是說,我可以在Linux上編譯和鏈接你的程序,做如下修改:

1)我將Complex的類定義移動到名爲header_complex.h的頭文件中,因爲這是主程序#include s。

2)我添加了默認構造函數的定義:

Complex::Complex() : real(0), imag(0) {} 

3)我加-lstdc++到命令行:

gcc complex.cpp -lstdc++ 

順便說一句,我想你會要修改你的顯示方法添加一些endl S:

cout << "Real part is " << real << endl; 
    cout << "Imaginary part is " << imag << endl; 
0

電解金屬錳...查看

Complex c1; // here's the error - you have no defined Complex::Complex() {} method