c++ - 温度转换程序c++

标签 c++ class temperature

我一直在尝试编写一个温度转换程序来自学 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

最佳答案

线

if (temperatureCalculator, setTemperature (InputReading, temperatureformat))

我觉得很可疑。为什么 temperatureCalculatorsetTemperature 之间有逗号?您是不是想用一个“点”(.) 来代替在您的 Temperature 类实例 temperatureCalculator< 上调用类方法 setTemperature/?

如果您发布实际错误,我们可以提供更多帮助。祝你好运!

关于c++ - 温度转换程序c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33587821/

相关文章:

c++ - 有没有人能够正确使用 libsensors?

r - 将 ASCII 格式的海面温度文本文件导入 R

c++ - 如何在 C++ 11 中迭代 std::tuple

c++ - 如何检测在 Windows 窗体消息框中按下了哪个按钮?

c++ - QTCPSocket同时由定时器启动

java - 如何使用 Class 的实例作为泛型类型?

c++ - 如何在没有函数调用的情况下返回值?

Mysql 查询显示的日期结果有点困惑

c++ - 在 Linux C++ 中获取 PTY 的最简单方法

c++ - 为什么只有在使用公共(public)继承时派生类的友元函数为 "available"?