简体   繁体   English

编译简单的C ++程序时遇到问题

[英]Problem compiling simple C++ progam

I was provided this simple C++ [I think] program to investigate the maximum size of int that can be stored: 提供了一个简单的C ++ [我认为]程序来调查可以存储的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;

}

I'm attempting to compile with: 我正在尝试使用:

gcc -o int_max int_max.cpp

But I get the following error: 但是我收到以下错误:

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

I tried adding a return 0 at the end of main but that didn't help. 我尝试在main的末尾添加return 0,但这没有帮助。 Any idea what I've done wrong? 知道我做错了什么吗?

PS It's possible this is actually a C snippet but I seem to remember the lecturer saying it was C++. PS可能这实际上是C代码段,但我似乎记得讲师说它是C ++。

Cheers 干杯

You are compiling C++ code with gcc in a file with .c extension? 您正在使用gcc在扩展名为.c的文件中编译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;
}

and use g++ to compile. 并使用g++进行编译。

This is a problem that I tend to refer to as error message blindness . 我倾向于将这个问题称为错误消息失明 Consider your first error: 考虑您的第一个错误:

int_max.cpp:4: error: '::main' must return 'int'

The error here is that main() must return int. 这里的错误是main()必须返回int。 You currently have main() declared as returning void. 您目前已将main()声明为返回void。

For this error message: 对于此错误消息:

int_max.cpp:8: error: 'cout' was not declared in this scope
int_max.cpp:8: error: 'endl' was not declared in this scope

All standard library functions and objects are contained within the std namespace. 所有标准库函数和对象都包含在std名称空间内。 That is, you can either: 也就是说,您可以:

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

or: 要么:

using namespace std;

...

cout << "whatever" << endl;

Finally: 最后:

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

You have deliberately overflowed integers in these expressions. 您故意在这些表达式中溢出了整数。 If you take the maximum number an integer can hold and add one to it, what happens? 如果您采用一个整数可以容纳的最大数量并加一个整数,会发生什么? The compiler is warning you that you've done that. 编译器警告您已完成该操作。

Change 更改

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

to

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

add a using statement after the includes: 在includes之后添加using语句:

using namespace std;

and at the end of main function: 在主要功能的末尾:

return EXIT_SUCCESS;

Change the line 换线

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

to

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

The first error is because the function main() is required by the C++ Standard to return an integer. 第一个错误是因为C ++ Standard要求函数main()返回整数。 Change to: 改成:

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

The remainder are because the objects named are in the std namespace. 剩下的是因为命名的对象在std名称空间中。 Add a using directive afuer your #includes: 在您的#includes中添加using指令:

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

The warnings are becaiuse when you say something like: 之所以说这些警告,是因为您说出类似以下内容:

INT_MAX + 1

you are trying to create a value that is bigger than the biggest possible integer. 您正在尝试创建一个大于最大可能整数的值。 This may or may not do what you want. 这可能会或可能不会做您想要的。

As Mehrdad has pointed out you are compiling compiling c++ code from a file with a .c extension. 正如Mehrdad指出的那样,您正在从扩展名为.c的文件中编译c ++代码。

Also you will need a "using namespace std;" 另外,您将需要一个“ using namespace std;”。 in after your #include if you aren't going to explicitly specify it for cout. 在#include之后,如果您不想为cout明确指定它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM