2

我知道温度计的问题已经死了,但我想我会试一试。

我不断收到错误消息“使用未声明的标识符'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);
};

转换器.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;
}
4

2 回答 2

0

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

在你的主目录中创建一个Thermometer实例怎么样?

Thermometer tm;
tm.converterc(degree, weehoo);
于 2013-05-06T02:04:02.910 回答
0

converterc并且converterf是您班级中的功能。这意味着它们可以在作为此类或派生自此的类的实例的对象上被调用。

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);
};
于 2013-05-06T02:11:32.803 回答