带有类错误消息的c ++温度计

时间:2013-05-06 02:01:07

标签: c++

我知道温度计问题已经完成,但我想我会试一试。

我不断收到错误消息“使用未声明的标识符'converterc'”和“使用未声明的标识符'converterf'”。有什么想法吗?

 #include <iostream>
#include "converters.h"
using namespace std;

int main()
{
    int degree;
    int weehoo;

    cout<<"\n\n\n\t\t Enter the temperature : ";
    cin>>degree;
    cout<<"\n\n\t\t If the temperature is in Celsius enter 0, if Farenheit enter 1 :";
    cin>>weehoo;

    if (weehoo==0)
    {
        cout<<"\n\n\t\tThe temperature in Farenheit is "<<converterc(degree,weehoo)<<endl;
    }
    else
    {
        cout<<"\n\n\t\tThe temperature in Celsius is "<<converterf(degree,weehoo)<<endl;
    }
    return 0;
}
#ifndef __again_converters_h
#define __again_converters_h
#endif
#pragma once

class Thermometer
{
private:
float degreeC;   //celcius
float degreeF;   //farenheit

public:
void setCelcius (float c) {degreeC=c;}
void setFarenheit (float f) {degreeF=f;}
float getCelcius (void){return degreeC;}
float getFarenheit (void){return degreeF;}
Thermometer  (float degree=0,float f=0, float c=0,float outtemp=0);
float converterc(int degree,int weehoo);
float converterf(int degree,int weehoo);
};

converters.cpp文件      #include“converters.h”

 float Thermometer::converterf(int degree,int weehoo)
{
degreeC=((degree-32) * (.5556));
return degreeC ;

}
float Thermometer::converterc(int degree,int weehoo)
{
degreeF=((1.8)*degree)+32;
return degreeF;
}

2 个答案:

答案 0 :(得分:0)

convertercconverterf是类Thermometer的成员函数,但您在没有Thermometer实例的情况下调用它们。

如何在主体中创建Thermometer实例?

Thermometer tm;
tm.converterc(degree, weehoo);

答案 1 :(得分:0)

convertercconverterf是您班级中的功能。这意味着它们可以被称为对象是此类的实例或从此派生的类。

class Thermometer
{
private:
float degreeC;   //celcius
float degreeF;   //farenheit
//...
public:
float converterc(int degree,int weehoo);
float converterf(int degree,int weehoo);
};

    int degree = 1;
    int weehoo = 2;
    Thermometer t; //initialize it properly if this is needed before calling functions
    float f = t.converterc(degree,weehooo);

以您执行此操作的方式使用这些功能:

float f = converterc(degree,weehooo);

可以是:

float f = Thermometer::converterc(degree,weehooo);

但是它们必须是静态这意味着它们没有this指针并且对于整个类是通用的(仍然可以使用类的实例调用它们但它是没必要):

class Thermometer
{
private:
float degreeC;   //celcius
float degreeF;   //farenheit
//...
public:
static float converterc(int degree,int weehoo);
static float converterf(int degree,int weehoo);
};