代码在 ideone 中编译,但不使用 gcc
Code compiles in ideone but not with gcc
我写了以下代码:
#include <iostream>
using namespace std;
int main()
{
int v()
return 0;
}
我在 ideone 中运行了它,它编译成功。我的计算机上的文件test1.cpp
中具有相同的代码,我运行g++ test1.cpp
并收到以下错误:
./test1.cpp: In function ‘int main()’:
./test1.cpp:7:2: error: a function-definition is not allowed here before ‘return’
为什么会这样?这是一个错误吗?我正在使用 linux mint,gcc 版本 4.7。
您在此处缺少分号:
int v()
^
应该是:
int v() ;
这是一个函数声明,不清楚这是意图。如果要初始化v
则以下方法有效:
int v(0) ;
或在 C++11 中:
int v{0} ;
这
通常被称为C++最烦人的解析。当你做类似的事情时
int f();
编译器将其读取为函数原型,声明返回int
的函数f
。如果您使用的是 C++11,则应改为
int f{}; // f initialized to 0
如果不使用 C++11,请确保立即初始化变量。
你忘记了后面的分号
int v();
Ideone 在使用 4.7 时将 gcc 4.8.1 用于您的代码(如您在自己的链接中看到的那样)
关于C++ 11 实现存在一些差异,显然它受到看起来像函数delcaration的行的影响。
相关文章:
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 奇怪的结构&GCC&clang(void*返回类型)
- GCC本机矩阵运算库
- PowerPC ppc64le上的Gcc Woverloaded虚拟错误
- gcc和c++17的过载解析失败
- 数据成员SFINAE的C++17测试:gcc vs clang
- GCC对可能有效的代码抛出init list生存期警告
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 使用gcc从静态链接的文件中查找可选符号
- 普通环路未使用gcc 4.8.5自动矢量化
- 有了gcc,是否可以链接库,但前提是它存在
- 在clang++预处理器中确定gcc工具链版本
- 为什么 gcc 编译这个而 msvc 没有
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- 我可以检测和更改 gcc/g++ 中结构的当前数据对齐设置吗?
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- 如何使用Clang/GCC在Mac上为C/C++设置VSCode
- 代码在 ideone 中编译,但不使用 gcc