简单C ++错误:“...未声明(首先使用此函数) [英] Simple C++ Error: "... undeclared (first use this function)"

查看:197
本文介绍了简单C ++错误:“...未声明(首先使用此函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始我的第一个C ++程序。由于某种原因,当我尝试编译它时,我得到以下错误:

 `truncate'undeclared(首先使用此函数) 

完整来源:

  #include< iostream> 
#include< math.h>

using namespace std;

#define CENTIMETERS_IN_INCH 2.54
#define POUNDS_IN_KILOGRAM 2.2

int main(){
double feet,inches,cm,weight_in_kg,weight_in_lbs;

//获取以英尺和英寸为单位的高度
cout< 输入高度(英尺):;
cin>>英尺
cout<< Enter(inches):;
cin>>英寸;

//将英尺和英寸转换为厘米
厘米=((12 *英尺)+英寸)* CENTIMETERS_IN_INCH;

// round 2个小数位和truncate
厘米= truncate(厘米);

printf(有人是%g'%g \将是%g cm tall,英尺,英寸,厘米);

// bmi的权重of 18.5
weight_in_kg = truncate(18.5 * centimeters);
weight_in_lbs = round(weight_in_kg * POUNDS_IN_KILOGRAM);

printf(18.5 BMI对应于约%g kg或% g lb,weight_in_kg,weight_in_lbs);

// bmi的权重为25
weight_in_kg = truncate(25 * cimeters);
weight_in_lbs = round(weight_in_kg * POUNDS_IN_KILOGRAM);

printf(25.0 BMI将对应于约%g kg或%g lbs,weight_in_kg,weight_in_lbs);

//暂停输出
cin> > feet;

return 0;
}

// round result
double round(double d){
return floor d + 0.5);
}

//舍入到小数点后1位
double truncate(double d){
return round(double * 10) 10;
}

任何帮助将不胜感激。

p>

  double truncate(double d); 
double round(double d);

您可以在main之前定义函数,这将解决问题:

  #include< iostream> 
#include< math.h>

using namespace std;

#define CENTIMETERS_IN_INCH 2.54
#define POUNDS_IN_KILOGRAM 2.2

//舍入结果
双回合(双d){
回到楼层d + 0.5);
}

//舍入和截断到小数点后一位
double truncate(double d){
return round(double * 10)/ 10;
}

int main(){
...
}


Hey guys, I am working on my first C++ program for school. For some reason I am getting the following error when I try to compile it:

`truncate' undeclared (first use this function)

Full Source:

#include <iostream>
#include <math.h>

using namespace std;

#define CENTIMETERS_IN_INCH 2.54
#define POUNDS_IN_KILOGRAM 2.2

int main() {
    double feet, inches, centimeters, weight_in_kg, weight_in_lbs;

    // get height in feet and inches
    cout << "Enter height (feet): ";
    cin >> feet;
    cout << "Enter (inches): ";
    cin >> inches;

    // convert feet and inches into centimeters
    centimeters = ((12 * feet) + inches) * CENTIMETERS_IN_INCH;

    // round 2 decimal places and truncate
    centimeters = truncate(centimeters);

    printf("Someone that is %g' %g\" would be %g cm tall", feet, inches, centimeters);

    // weights for bmi of 18.5
    weight_in_kg = truncate(18.5 * centimeters);
    weight_in_lbs = round(weight_in_kg * POUNDS_IN_KILOGRAM);

    printf("18.5 BMI would correspond to about %g kg or %g lbs", weight_in_kg,   weight_in_lbs);

    // weights for bmi of 25
    weight_in_kg = truncate(25 * centimeters);
    weight_in_lbs = round(weight_in_kg * POUNDS_IN_KILOGRAM);

    printf("25.0 BMI would correspond to about %g kg or %g lbs", weight_in_kg, weight_in_lbs);

    // pause output
    cin >> feet;

    return 0;
}

// round result
double round(double d) {
   return floor(d + 0.5);
}

// round and truncate to 1 decimal place
double truncate(double d) {
   return round(double * 10) / 10;
}

Any help would be appreciated. Thanks.

解决方案

You need forward declaration before your main:

double truncate(double d);
double round(double d);

You could just define your functions before main, that will solve the problem too:

#include <iostream>
#include <math.h>

using namespace std;

#define CENTIMETERS_IN_INCH 2.54
#define POUNDS_IN_KILOGRAM 2.2

// round result
double round(double d) {
   return floor(d + 0.5);
}

// round and truncate to 1 decimal place
double truncate(double d) {
   return round(double * 10) / 10;
}

int main() {
...
}

这篇关于简单C ++错误:“...未声明(首先使用此函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆