我一直在嘗試編寫一個溫度轉換程序來教自己如何在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
帶行號的編譯錯誤列表將幫助 –
開始將主類放在類外 – wimh
_'class「Temperature」沒有成員「getCelsius」_ _所以你可以添加一個成員'getCelsius'? – wimh