编译简单C++;节目 我提供了这个简单的C++(i)程序来研究可以存储的int的最大大小: #include <limits.h> #include <iostream> void main ( int argc , char * argv[]) { cout << "INT_MAX " << INT_MAX << endl ; cout << "INT_MAX +1 = " << INT_MAX + 1 << endl ; cout << "INT_MAX -1 = " << INT_MAX - 1 << endl ; cout << "INT_MAX / INT_MAX " << INT_MAX /INT_MAX << endl ; cout << "(INT_MAX +1) / INT_MAX " << (INT_MAX +1) /INT_MAX << endl; cout << "(INT_MAX -1) / INT_MAX " << (INT_MAX -1) /INT_MAX <<endl; cout << "INT_MAX / (INT_MAX +1) " << INT_MAX /(INT_MAX+1) <<endl; cout << "INT_MAX / (INT_MAX -1) " << INT_MAX /(INT_MAX -1) <<endl; }

编译简单C++;节目 我提供了这个简单的C++(i)程序来研究可以存储的int的最大大小: #include <limits.h> #include <iostream> void main ( int argc , char * argv[]) { cout << "INT_MAX " << INT_MAX << endl ; cout << "INT_MAX +1 = " << INT_MAX + 1 << endl ; cout << "INT_MAX -1 = " << INT_MAX - 1 << endl ; cout << "INT_MAX / INT_MAX " << INT_MAX /INT_MAX << endl ; cout << "(INT_MAX +1) / INT_MAX " << (INT_MAX +1) /INT_MAX << endl; cout << "(INT_MAX -1) / INT_MAX " << (INT_MAX -1) /INT_MAX <<endl; cout << "INT_MAX / (INT_MAX +1) " << INT_MAX /(INT_MAX+1) <<endl; cout << "INT_MAX / (INT_MAX -1) " << INT_MAX /(INT_MAX -1) <<endl; },c++,compiler-errors,C++,Compiler Errors,但我得到了以下错误: int_max.cpp:4: error: '::main' must return 'int' int_max.cpp: In function 'int main(int, char**)': int_max.cpp:8: error: 'cout' was not declared in this scope int_max.cpp:8: error: 'endl' was not declared in this scope int_max.cpp:9: warni

但我得到了以下错误:

int_max.cpp:4: error: '::main' must return 'int'
int_max.cpp: In function 'int main(int, char**)':
int_max.cpp:8: error: 'cout' was not declared in this scope
int_max.cpp:8: error: 'endl' was not declared in this scope
int_max.cpp:9: warning: integer overflow in expression
int_max.cpp:13: warning: integer overflow in expression
int_max.cpp:15: warning: integer overflow in expression
我尝试在main的末尾添加一个返回0,但没有效果。知道我做错了什么吗

可能这实际上是一个C片段,但我似乎记得讲师说它是C++。

欢呼声

< p>你在用<代码> > c>代码>扩展>

文件中编译C++代码,代码为<>代码> gcc>代码>
// Use new C++ header files instead of their .h version.
#include <climits>
#include <iostream>

// cout and endl are declared in the std namespace.
using namespace std;

int main (int argc, char * argv[])    
{
  cout << "INT_MAX      " << INT_MAX   << endl ;
  cout << "INT_MAX +1 = " << INT_MAX + 1  << endl ;
  cout << "INT_MAX -1 = " << INT_MAX - 1 << endl ;

  cout << "INT_MAX / INT_MAX        " << INT_MAX /INT_MAX << endl ;
  cout << "(INT_MAX +1) / INT_MAX   " << (INT_MAX +1) /INT_MAX << endl;
  cout << "(INT_MAX -1) / INT_MAX   " << (INT_MAX -1) /INT_MAX  <<endl;
  cout << "INT_MAX / (INT_MAX +1)   " << INT_MAX  /(INT_MAX+1)  <<endl;
  cout << "INT_MAX / (INT_MAX -1)   " << INT_MAX  /(INT_MAX -1)  <<endl;

  return 0;
}
<代码> /使用新的C++头文件代替它们的.h版本。 #包括 #包括 //cout和endl在std名称空间中声明。 使用名称空间std; int main(int argc,char*argv[]) {
CUT< P>您在代码< >代码>扩展>

文件中编译代码为<代码> GCC < /代码> C++代码>
// Use new C++ header files instead of their .h version.
#include <climits>
#include <iostream>

// cout and endl are declared in the std namespace.
using namespace std;

int main (int argc, char * argv[])    
{
  cout << "INT_MAX      " << INT_MAX   << endl ;
  cout << "INT_MAX +1 = " << INT_MAX + 1  << endl ;
  cout << "INT_MAX -1 = " << INT_MAX - 1 << endl ;

  cout << "INT_MAX / INT_MAX        " << INT_MAX /INT_MAX << endl ;
  cout << "(INT_MAX +1) / INT_MAX   " << (INT_MAX +1) /INT_MAX << endl;
  cout << "(INT_MAX -1) / INT_MAX   " << (INT_MAX -1) /INT_MAX  <<endl;
  cout << "INT_MAX / (INT_MAX +1)   " << INT_MAX  /(INT_MAX+1)  <<endl;
  cout << "INT_MAX / (INT_MAX -1)   " << INT_MAX  /(INT_MAX -1)  <<endl;

  return 0;
}
<代码> /使用新的C++头文件代替它们的.h版本。 #包括 #包括 //cout和endl在std名称空间中声明。 使用名称空间std; int main(int argc,char*argv[]) { 不能改变线路吗

void main ( int argc , char * argv[])
void main ( int argc , char * argv[])

换线

void main ( int argc , char * argv[])
void main ( int argc , char * argv[])


第一个错误是因为C++函数要求函数主体()需要返回一个整数。

 int main ( int argc , char * argv[])
其余部分是因为命名的对象位于
std
命名空间中。请添加一个using指令,其中包括:

#include <limits.h>
#include <iostream>
using namespace std;
using namespace std;

您试图创建一个大于最大可能整数的值。这可能或不可能做您想要的。

第一个错误是因为C++(c++)标准要求函数主体()需要返回一个整数。
 int main ( int argc , char * argv[])
其余部分是因为命名的对象位于
std
命名空间中。请添加一个using指令,其中包括:

#include <limits.h>
#include <iostream>
using namespace std;
using namespace std;
您正在尝试创建一个大于最大可能整数的值。这可能会也可能不会满足您的要求。

更改

在包含以下内容后添加using语句:

#include <limits.h>
#include <iostream>
using namespace std;
using namespace std;
在主要功能的末尾:

return EXIT_SUCCESS;
改变

在包含以下内容后添加using语句:

#include <limits.h>
#include <iostream>
using namespace std;
using namespace std;
在主要功能的末尾:

return EXIT_SUCCESS;

正如Mehrdad指出的,你正在编译一个带有C扩展名的C++代码。


您还需要一个“使用命名空间STD”;在您的“包含”中,如果您不为CUT显式指定它。

< P>,Mehrdad指出,您正在编译一个具有C扩展名的文件中的C++代码。

您还需要一个“使用命名空间STD”;在您的“包含”中,如果您不为CUT显式指定它。

< P>这是一个我倾向于称之为错误消息盲的问题。考虑您的第一个错误:

int_max.cpp:4: error: '::main' must return 'int'
这里的错误是main()必须返回int。您当前将main()声明为返回void

对于此错误消息:

int_max.cpp:8: error: 'cout' was not declared in this scope
int_max.cpp:8: error: 'endl' was not declared in this scope
所有标准库函数和对象都包含在
std
命名空间中。也就是说,您可以:

std::cout << "whatever" << std::endl;

你在这些表达式中故意溢出整数。如果你取一个整数的最大值并加上一个,那么会发生什么?编译器警告你已经完成了。

< P>这是一个我倾向于称之为错误消息盲的问题。考虑你的第一个错误:

int_max.cpp:4: error: '::main' must return 'int'
这里的错误是main()必须返回int。您当前将main()声明为返回void

对于此错误消息:

int_max.cpp:8: error: 'cout' was not declared in this scope
int_max.cpp:8: error: 'endl' was not declared in this scope
所有标准库函数和对象都包含在
std
命名空间中。也就是说,您可以:

std::cout << "whatever" << std::endl;


您故意在这些表达式中溢出了整数。如果您取整数可以容纳的最大值并向其中添加一个,会发生什么情况?编译器会警告您这样做。

将void main(…)更改为int main(…)此外,如果它的C++从极限标头中查看数值限制类,则包含(而不是)包含。它允许你编写诸如STD::NoimiCiLime::Max()的东西,而不是INTXMAX。此外,如果它的C++从限制标头中查看数值限制类,则包含了,而不是“包含”。它允许你编写诸如STD::NoimiCiLime::Max()而不是InthMax。返回语句在<代码>主()/<代码>中是可选的;declaration无需显式返回任何内容。检查:@Manuel:无需在语法上,但在语义上,不返回任何内容表示“我不知道应用程序停止运行时处于什么状态”,而
return 0;
表示“我的应用程序已成功运行”你想向你的维护程序员传达什么信息?@Bill:fall-through的主要意思是“成功”。我希望我的维护程序员熟悉这个惯例。@Manuel:fall-through的任何一个意思都是“成功”或“我忘了在这里放东西”…您如何通过查看外部代码来判断哪个是?返回语句在
main()
中是可选的,投诉是由于
void main()
声明不需要显式返回任何内容。检查:@Manuel:不需要在语法上返回,但在语义上不返回任何内容“我不知道我的应用程序停止运行时处于什么状态”,而
返回0;
表示“我的应用程序运行成功”。您想向您的维护程序员发送什么消息?@Bill:fall-through的主要意思是“成功”“。我希望我的维护程序员熟悉这个惯例。@Manuel:fall-through的意思是“成功”或“我忘了在这里放东西了”…您如何通过查看外部代码来区分哪个?事实上,没有必要显式返回0。请检查此项:基于注释的更新答案。事实上,没有必要显式返回0。请检查此项:基于注释的更新答案。抱歉,没有,我以.c和.cpp的形式尝试了此选项,复制/粘贴了错误的示例。我还处于印象:GCC编译C++也有?@亚当:我总是遇到编译C++ COD的问题。