简单的 C++ 错误:“...未声明(首先使用此函数)”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1354522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple C++ Error: "... undeclared (first use this function)"
提问by Pooch
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:
大家好,我正在为学校编写我的第一个 C++ 程序。出于某种原因,当我尝试编译它时出现以下错误:
`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.
任何帮助,将不胜感激。谢谢。
回答by AraK
You need forward declaration before your main
:
您需要在您的main
:
double truncate(double d);
double round(double d);
You could just define your functions before main, that will solve the problem too:
您可以在 main 之前定义您的函数,这也可以解决问题:
#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() {
...
}
回答by Jonathan Leffler
You are attempting to call a function truncate()
at:
您正在尝试在以下位置调用函数truncate()
:
centimeters = truncate(centimeters);
You have not yet told the compiler what that function is, so it is undefined and the compiler is objecting.
你还没有告诉编译器那个函数是什么,所以它是未定义的,编译器反对。
In C++, all functions must be declared (or defined) before they are used. If you think you are using a standard C++ library function, you need to include its header. If you are not sure that you are using a C++ library function, you need to declare and define your own.
在 C++ 中,所有函数在使用之前都必须声明(或定义)。如果您认为您使用的是标准 C++ 库函数,则需要包含其头文件。如果您不确定使用的是 C++ 库函数,则需要声明和定义自己的库函数。
Be aware that on POSIX-based systems, truncate()
is a system call that truncates an existing file; it will have a different prototype from what you are trying to use.
请注意,在基于 POSIX 的系统上,truncate()
是一个截断现有文件的系统调用;它将具有与您尝试使用的原型不同的原型。
Further down your code - hidden off the bottom of the scroll bar - are the function definitions for truncate()
and round()
. Put the function definitions at the top of the file, so that the compiler knows about their signature before they are used. Or add forward declarations of the functions at the top of the file and leave the definitions where they are.
再往下,你的代码-隐藏关滚动条的底部-是该函数的定义truncate()
和round()
。将函数定义放在文件的顶部,以便编译器在使用它们之前知道它们的签名。或者在文件顶部添加函数的前向声明,并将定义保留在原处。
回答by a2800276
You need to declare functions before you use them. Moving the definitions of truncate
and round
above the main
function should do the trick.
在使用它们之前,您需要声明函数。移动函数truncate
和round
上方的main
定义应该可以解决问题。