2015-11-07 471 views
0

我一直在嘗試編寫一個溫度轉換程序來教自己如何在C++中將任何給定的溫度輸入(攝氏度,華氏度和開氏溫度)轉換爲另外兩個值然後輸出所有三個值再次。然而,我無法讓我的項目進行編譯,而我自己也很難找到錯誤。 (相信我,我已經嘗試了一整天)。 如果有人可以請看看它,並提出任何建議/改進,我將不勝感激。順便說一句,不要在我的代碼中寫任何註釋。溫度轉換程序C++

#include <iostream> 
#include <cstring> 
#include <fstream> 
#include <cstdlib> 
#include <string> 

using namespace std; 

class Temperature 
{ 
private: 
    const double ConversionRate = 0.55555; 
    const double AbsoluteZero = 273.15; 
    const int TemperatureOffset = 32; 

    double InputTemperature = 0; 
    double Fahrenheit = 0, Celsius = 0, Kelvin = 0; 

public: 
    bool setTemperature(double temperature, char temperatureformat) 
    { 
     bool temperatureConfigured = true; 
     if (temperatureformat == 'c') { 
      Celsius = temperature; 
      Fahrenheit = ((1/ConversionRate)*(Celsius)) + TemperatureOffset; 
      Kelvin = Celsius + AbsoluteZero; 
     } 
     else if (temperatureformat == 'f') { 
      Fahrenheit = temperature; 
      Celsius = (ConversionRate*(Fahrenheit - TemperatureOffset)); 
      Kelvin = (Celsius + AbsoluteZero); 
     } 
     else if (temperatureformat == 'k') { 
      if (temperature >= 0) 
      { 
       Kelvin = temperature; 
       Celsius = Kelvin - AbsoluteZero; 
       Fahrenheit = (1/ConversionRate)*Celsius + TemperatureOffset; 
      } 
      else { 
       temperatureConfigured = false; 
       Celsius = 0; 
       Kelvin = 0; 
       Fahrenheit = 0; 

      } 
     } 
     else { 
      temperatureConfigured = false; 
     } 
    } 

    int main() 
    { 
     double InputReading = 0; 
     Temperature temperatureCalculator; 
     char temperatureformat = NULL; 

     cout << "Please Enter your temperature value" << endl; 
     cin >> InputReading; 
     cout << "Celsius  - c" << endl; 
     cout << "Fahrenheit  - f" << endl; 
     cout << "Kelvin   - k" << endl; 
     cin >> temperatureformat; 


     if (temperatureCalculator.setTemperature (InputReading, temperatureformat)) { 
      cout << "Your temperature conversions are" << endl; 
      cout << "Celsius: " << temperatureCalculator.getCelsius() << endl; 
      cout << "Fahrenheit: " << temperatureCalculator.getFahrenheit << endl; 
      cout << "Kelvin:  " << temperatureCalculator.getKelvin << endl; 
     } 
     else { 
      cout << "Error, your input was invalid" << endl; 
     } 
    } 
} 

編譯錯誤如下:

Severity Code Description Project File Line 
Error (active)  class "Temperature" has no member "getCelsius" 69 
Error (active)  class "Temperature" has no member "getFahrenheit" 70 
Error (active)  class "Temperature" has no member "getKelvin" 71 
Error (active)  expected a ';' 77 
Error C1004 unexpected end-of-file found 78 
+1

帶行號的編譯錯誤列表將幫助 –

+1

開始將主類放在類外 – wimh

+1

_'class「Temperature」沒有成員「getCelsius」_ _所以你可以添加一個成員'getCelsius'? – wimh

回答

0

有幾個問題:

  • Java不同,main將從類分開。

  • 您錯過了get您嘗試使用的方法。

  • 你有幾個錯別字。

  • 我不確定你是否真的想要私人常量。宏會更有意義。

無論如何,這裏的代碼(編譯)。它沒有經過測試,所以這只是一個練習。

#include <iostream> 
#include <cstring> 
#include <fstream> 
#include <cstdlib> 
#include <string> 

using namespace std; 

class Temperature 
{ 
private: 
    const double ConversionRate = 0.55555; 
    const double AbsoluteZero = 273.15; 
    const int TemperatureOffset = 32; 

    double InputTemperature = 0; 
    double Fahrenheit = 0, Celsius = 0, Kelvin = 0; 

public: 
    double getFahrenheit() { return Fahrenheit; } 
    double getCelsius() { return Celsius; } 
    double getKelvin() { return Kelvin; } 
    bool setTemperature(double temperature, char temperatureformat) 
    { 
     bool temperatureConfigured = true; 
     if (temperatureformat == 'c') { 
      Celsius = temperature; 
      Fahrenheit = ((1/ConversionRate)*(Celsius)) + TemperatureOffset; 
      Kelvin = Celsius + AbsoluteZero; 
     } 
     else if (temperatureformat == 'f') { 
      Fahrenheit = temperature; 
      Celsius = (ConversionRate*(Fahrenheit - TemperatureOffset)); 
      Kelvin = (Celsius + AbsoluteZero); 
     } 
     else if (temperatureformat == 'k') { 
      if (temperature >= 0) 
      { 
       Kelvin = temperature; 
       Celsius = Kelvin - AbsoluteZero; 
       Fahrenheit = (1/ConversionRate)*Celsius + TemperatureOffset; 
      } 
      else { 
       temperatureConfigured = false; 
       Celsius = 0; 
       Kelvin = 0; 
       Fahrenheit = 0; 

      } 
     } 
     else { 
      temperatureConfigured = false; 
     } 
    } 

}; 

int main() 
    { 
     double InputReading = 0; 
     Temperature temperatureCalculator; 
     char temperatureformat = NULL; 

     cout << "Please Enter your temperature value" << endl; 
     cin >> InputReading; 
     cout << "Celsius  - c" << endl; 
     cout << "Fahrenheit  - f" << endl; 
     cout << "Kelvin   - k" << endl; 
     cin >> temperatureformat; 


     if (temperatureCalculator.setTemperature (InputReading, temperatureformat)) { 
      cout << "Your temperature conversions are" << endl; 
      cout << "Celsius: " << temperatureCalculator.getCelsius() << endl; 
      cout << "Fahrenheit: " << temperatureCalculator.getFahrenheit() << endl; 
      cout << "Kelvin:  " << temperatureCalculator.getKelvin() << endl; 
     } 
     else { 
      cout << "Error, your input was invalid" << endl; 
     } 
    } 
+0

請注意,'setTemperature'不會返回任何內容 - 這將會出現問題。 – erip

+0

非常感謝您......非常感謝您花時間幫助! –

1

if (temperatureCalculator, setTemperature (InputReading, temperatureformat)) 

顯得可疑了我。爲什麼在temperatureCalculatorsetTemperature之間有逗號?你是否想要在你的Temperature類實例temperatureCalculator上使用'點'(.)而不是調用類方法setTemperature

如果您發佈實際錯誤,我們可以提供更多幫助。祝你好運!

+0

非常感謝,這是我的錯誤。我用一個點替換了逗號,但它沒有改變我從編譯器收到的錯誤信息。 :/ –

+0

正如其他人提到的那樣,您需要 - 在類定義的末尾放置一個半冒號';'(在上面貼出的代碼中,緊跟在最後一行的右括號'}'後面 - 將你的'main'塊剪切並粘貼到類定義之外(將'main'放在類定義之下) - 在類getCelsius,getFahrenheit和getKelvin中定義它們 - 現在可以爲空,但爲每個編寫一個框架以使編譯器開心 –

1

這條線,也許?

if (temperatureCalculator, setTemperature (InputReading, temperatureformat)) 

我不確定你在用那個逗號做什麼。你可能想要放一段時間。您還需要在課程末尾放置分號(在最後一個大括號之後)。最後,你應該把你的主要方法放在文件末尾,在課堂之外。

0

你有很多問題,你的代碼:

  1. 更改轉換常數static(不是一個真正的問題,但與C++ 03警告)
  2. 初始化在構造函數中的局部變量,而不是在課堂上定義。 (在C++ 03中警告)
  3. 爲本地變量定義getter。
  4. setTemperature()返回值。
  5. 在班級以外移動main()的定義。
  6. 在類定義之後添加分號。
  7. 更改int main()int main(void)
  8. 使用char temperatureformat = 0而不是char temperatureformat = NULL
  9. 更改, setTemperature.setTemperature
  10. 在函數名後加上括號(必需)。
  11. 添加main()的返回值。

所有這些都是由CHANGE:

#include <iostream> 
#include <cstring> 
#include <fstream> 
#include <cstdlib> 
#include <string> 

using namespace std; 

class Temperature 
{ 
private: 
    // CHANGE: DECLARE STATIC 
    static const double ConversionRate = 0.55555; 
    static const double AbsoluteZero = 273.15; 
    static const int TemperatureOffset = 32; 

    // CHANGE: USE CONSTRUCTOR BELOW INSTEAD FOR INITIALIZATION 
    double InputTemperature; 
    double Fahrenheit, Celsius, Kelvin; 

public: 
    // CHANGE: DEFINE CONSTRUCTOR 
    Temperature() : InputTemperature(0), Fahrenheit(0), Celsius(0), Kelvin(0) {} 

    // CHANGE: DEFINE GETTERS 
    double getFahrenheit() const {return Fahrenheit;} 
    double getCelsius() const {return Celsius;} 
    double getKelvin() const {return Kelvin;} 

    bool setTemperature(double temperature, char temperatureformat) 
    { 
     bool temperatureConfigured = true; 
     if (temperatureformat == 'c') { 
      Celsius = temperature; 
      Fahrenheit = ((1/ConversionRate)*(Celsius)) + TemperatureOffset; 
      Kelvin = Celsius + AbsoluteZero; 
     } 
     else if (temperatureformat == 'f') { 
      Fahrenheit = temperature; 
      Celsius = (ConversionRate*(Fahrenheit - TemperatureOffset)); 
      Kelvin = (Celsius + AbsoluteZero); 
     } 
     else if (temperatureformat == 'k') { 
      if (temperature >= 0) 
      { 
       Kelvin = temperature; 
       Celsius = Kelvin - AbsoluteZero; 
       Fahrenheit = (1/ConversionRate)*Celsius + TemperatureOffset; 
      } 
      else { 
       temperatureConfigured = false; 
       Celsius = 0; 
       Kelvin = 0; 
       Fahrenheit = 0; 

      } 
     } 
     else { 
      temperatureConfigured = false; 
     } 

     // CHANGE: RETURN VALUE 
     return temperatureConfigured; 
    } 

    // CHANGE: MOVE main() OUTSIDE CLASS 
}; // CHANGE: ADD SEMICOLON AFTER CLASS 

// CHANGE: MAKE int main(void) 
int main(void) 
{ 
    double InputReading = 0; 
    Temperature temperatureCalculator; 
    // CHANGE: MAKE 0, NOT NULL 
    char temperatureformat = 0; 

    cout << "Please Enter your temperature value" << endl; 
    cin >> InputReading; 
    cout << "Celsius  - c" << endl; 
    cout << "Fahrenheit  - f" << endl; 
    cout << "Kelvin   - k" << endl; 
    cin >> temperatureformat; 

    // CHANGE: USE . MEMBER ACCESS OPERATOR 
    if (temperatureCalculator.setTemperature (InputReading, temperatureformat)) { 
     cout << "Your temperature conversions are" << endl; 
     cout << "Celsius: " << temperatureCalculator.getCelsius() << endl; 
     // CHANGE: ADD PARENTHESES AFTER FUNCTION NAMES 
     cout << "Fahrenheit: " << temperatureCalculator.getFahrenheit() << endl; 
     cout << "Kelvin:  " << temperatureCalculator.getKelvin() << endl; 
    } 
    else { 
     cout << "Error, your input was invalid" << endl; 
    } 

    // CHANGE: RETURN VALUE 
    return 0; 
} 
0

前綴的一些函數的定義與;添加到您的除了班級在上課結束評論:

錯誤(活動)班「溫度「has no member」getCelsius「69

double getCelsius() 
    { 
     return Celsius; 
    } 

誤差(活性)類 「溫度」 沒有成員 「getFahrenheit」 70

double getFahrenheit() 
{ 
    return Fahrenheit; 
} 

誤差(活性)類 「溫度」 沒有成員 「getKelvin」 71

double getKelvin() 
{ 
    return Kelvin; 
}