Ada*_*lor 1 c++ compiler-errors
我提供了这个简单的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;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试编译:
gcc -o int_max int_max.cpp
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
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
Run Code Online (Sandbox Code Playgroud)
我尝试在main结束时添加一个返回0,但这没有帮助.知道我做错了什么吗?
PS这可能实际上是一个C片段,但我似乎记得讲师说它是C++.
干杯
您正在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;
}
Run Code Online (Sandbox Code Playgroud)
并用于g++编译.
| 归档时间: |
|
| 查看次数: |
6594 次 |
| 最近记录: |