2017-04-21 104 views
-1

在Microsoft Visual Studio中處理類項目時,我遇到以下錯誤:
運算符'+'不能被施加到類型 '字符串' 和 '方法組'錯誤消息:運算符'+'不能應用於類型'字符串'和'方法組'的操作數

的操作數這些是代碼中的相關部分:

public double CalculateTotalFuelCost() 
    { 
     double CalculateTotalFuelCost = gallonsConsumed * fuelCostPerGallon; 
     return CalculateTotalFuelCost; 
    } 

public override string ToString() 
    { 
     return "Destination: " + destination + "\nTotal Miles: " + distanceTraveled + "\nFuel Consumed: " + gallonsConsumed + " gallons" + "\nFuel Cost Per Gallon: $" + 
      fuelCostPerGallon + "\nTotal Fuel Cost for this Trip: $" + 
CalculateTotalFuelCost + "\nMPG: " + CalculateMilesPerGallon + 
      "\n Fuel Cost Per Mile: $" + CalculateCostPerMile; 
    } 

該錯誤涵蓋了上述代碼的這個小片段。

+ "\nTotal Fuel Cost for this Trip: $" + CalculateTotalFuelCost 

其他都顯示沒有錯誤。 我不知道我在做什麼錯。感謝您提供任何幫助和建議。

+0

當你看到'方法組'時,認爲'缺少括號' – Jonesopolis

+1

這不是VB,兒子! – Will

+0

[Operator'+'的可能重複不能應用於「string」和「method group」類型的操作數](http://stackoverflow.com/questions/22066962/operator-cannot-be-applied-to-operands- of-type-string-and-method-group) –

回答

2

它是一個函數,你忘了()

CalculateTotalFuelCost() 

執行相同的所有你想調用其他功能。

+0

修復了錯誤!感謝您的幫助! –

+0

@KyleW不客氣:)不要忘記[接受](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)答案,如果它解決了你的問題問題。 – CNuts

相關問題