2014-02-27 78 views
1

我知道我的工作是草率的,這是我在這堂課中的第四個任務。任何幫助將不勝感激,謝謝。錯誤:無法在作業中將'double'轉換爲'double(double,double,double)'

double getPrincipal(0); 
double getRate(0); 
double getYears(0); 
double computeAmount(double getPrincipal, double getRate, double getYears); 
double displayAmount(double principal, double rate, double years, double amount); 

cout << "what is the principal ammount?" << endl; 
cin >> getPrincipal; 

cout << "What is the percentage rate?" << endl; 
cin >> getRate; 

cout << "Over how many years will the money stay in the bank?" << endl; 
cin >> getYears; 

computeAmount = pow((1 + getRate/100),getYears); // This is where i got the error 
+0

您的意思是聲明在此代碼塊/使用lambda函數? –

回答

1

當編譯器是想告訴你,你不能一個變量如果你想這是一個函數分配給功能

,定義它&調用它。

如果您希望它是一個變量,請將其聲明爲變量。

3

您試圖通過分配一個值的函數與variables搞亂functions

double computeAmount(double getPrincipal, double getRate, double getYears); 

通過這條線,你聲明computeAmount()是誰需要3個double S作爲它的參數和返回double的功能。

但是,在這條線上,

computeAmount = pow((1 + getRate/100),getYears); 

你試圖使用它作爲一個變量。

取決於你的目的是什麼,你可能想要改變這兩行中的一行。例如,可以刪除第一行,第二行更改爲:

double computeAmount = pow((1 + getRate/100),getYears); 
+0

_'You can not assign a value to a function.'_呃,實際上你可以:'virtual void foo()= 0;' –

+1

@πάνταῥεῖ這不是一個賦值,而是一種指定純虛擬的語法方式功能。 –

+0

@ZacHowland你實際上可以使用「0」以外的值(甚至對此也有合理的用例)。 –

1

computeAmount是你定義一個返回double和需要3個double參數的函數的名稱。 pow返回double

把上面一行

double computedAmount = pow((1 + getRate)/100, getYears); 
     ^^^^^^^^^^^^^^ -- notice this is no longer the function name, but a new variable 
1

你聲明的名稱computeAmount的函數名

double computeAmount(double getPrincipal, double getRate, double getYears); 

所以這種說法

computeAmount = pow((1 + getRate/100),getYears); 

有沒有意義。因爲computeAmount是一個函數名,那麼在上面的espression中將它轉換爲指向函數的指針,並且您試圖將函數pow返回的某個double值分配給此指針。

1

computeAmount被聲明爲一個函數,但用於'='運算符的左側。 解決辦法:重新申報computeAmount只是一個雙:

double computeAmount; 
相關問題