简体   繁体   中英

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:

#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. 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++.

Cheers

You are compiling C++ code with gcc in a file with .c extension?

// 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.

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. You currently have main() declared as returning 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. 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:

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. Change to:

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

The remainder are because the objects named are in the std namespace. Add a using directive afuer your #includes:

#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.

Also you will need a "using namespace std;" in after your #include if you aren't going to explicitly specify it for cout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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