如何编写c++;升华2中的方法 这是我写的简单C++程序。我试图从命令行传入一个参数。当我试图从命令行编译它时,它说有一个未定义的标识符“kgToLbs”。我认为这是可以接受的C++程序。我必须以不同的方式在Sublime 2中编写方法吗 #include <iostream> #include <string> #include <iomanip> /* This program takes a numerical number from the command line and converts it into a numberical representation in lbs. Then it displays the number to std output with a precision of 2 decimal places */ using namespace std; const double CONVERSION = 2.2046226218487757; /* Convert Method This method converts from kg to lbs Parameter: A double Returns: A double */ double kgToLbs(double num) { double resultInPounds = num * CONVERSION; return resultInPounds; } int main(int argc, char *argv[]) { double numInLbs = 0; //Take the number passed in from the command line //Convert it to pounds numInLbs = kgToLbs(argv[0]); //Output the converted number to cout with a precision of 2 //decimal places cout << setprecision(2) << numInLbs << endl; } #包括 #包括 #包括 /* 此程序从 命令行并将其转换为数字 以磅表示。 然后,它显示std输出的编号 小数点后2位的精度 */ 使用名称空间std; 常数双转换=2.2046226218487757; /* 转换方法 此方法将kg转换为lbs 参数:双精度 退货:双倍 */ 双倍重量(双倍数量) { double resultInPounds=num*换算; 返回结果磅; } int main(int argc,char*argv[]) { 双numInLbs=0; //获取从命令行传入的数字 //把它换算成英镑 numInLbs=kgToLbs(argv[0]); //将转换后的数字输出到cout,精度为2 //小数位 cout

如何编写c++;升华2中的方法 这是我写的简单C++程序。我试图从命令行传入一个参数。当我试图从命令行编译它时,它说有一个未定义的标识符“kgToLbs”。我认为这是可以接受的C++程序。我必须以不同的方式在Sublime 2中编写方法吗 #include <iostream> #include <string> #include <iomanip> /* This program takes a numerical number from the command line and converts it into a numberical representation in lbs. Then it displays the number to std output with a precision of 2 decimal places */ using namespace std; const double CONVERSION = 2.2046226218487757; /* Convert Method This method converts from kg to lbs Parameter: A double Returns: A double */ double kgToLbs(double num) { double resultInPounds = num * CONVERSION; return resultInPounds; } int main(int argc, char *argv[]) { double numInLbs = 0; //Take the number passed in from the command line //Convert it to pounds numInLbs = kgToLbs(argv[0]); //Output the converted number to cout with a precision of 2 //decimal places cout << setprecision(2) << numInLbs << endl; } #包括 #包括 #包括 /* 此程序从 命令行并将其转换为数字 以磅表示。 然后,它显示std输出的编号 小数点后2位的精度 */ 使用名称空间std; 常数双转换=2.2046226218487757; /* 转换方法 此方法将kg转换为lbs 参数:双精度 退货:双倍 */ 双倍重量(双倍数量) { double resultInPounds=num*换算; 返回结果磅; } int main(int argc,char*argv[]) { 双numInLbs=0; //获取从命令行传入的数字 //把它换算成英镑 numInLbs=kgToLbs(argv[0]); //将转换后的数字输出到cout,精度为2 //小数位 cout,c++,C++,argv[0]是一个char*,但是kgToLbs需要一个double。您需要将输入转换为double,然后才能在该上下文中使用它。您能显示COM吗?您曾经编译过它吗?这个问题与Sublime文本无关。Sublime是一个编辑器(你可以使用其他的东西,例如)你的代码可能是由一个编译器处理的,比如使用g++commandCompile和所有警告和调试信息(例如g++-Wall-Wextra-g),然后使用调试器(gdb);你的问题是一个修复我的代码,所以这里没有主题。这是正确的,但它应该编译,ch

argv[0]
是一个
char*
,但是
kgToLbs
需要一个
double
。您需要将输入转换为
double
,然后才能在该上下文中使用它。

您能显示COM吗?您曾经编译过它吗?这个问题与Sublime文本无关。Sublime是一个编辑器(你可以使用其他的东西,例如)你的代码可能是由一个编译器处理的,比如使用
g++
commandCompile和所有警告和调试信息(例如
g++-Wall-Wextra-g
),然后使用调试器(
gdb
);你的问题是一个修复我的代码,所以这里没有主题。这是正确的,但它应该编译,char*将被转换为一个双精度,产生错误的结果,但不会抱怨AO,但kgToLbs不存在。我得到了它来编译。我必须取消引用指针numInLbs=kgToLbs(*argv[0]);这就成功了我如何将参数从命令行传递到我的程序?@CharlieBarber:使用
main
的参数,因为它们是程序的参数,通常在命令行上传递(它们在POSIX系统上由shell全局传递)