[英]Code compiles in ideone but not with gcc
I wrote the following code: 我写了以下代码:
#include <iostream>
using namespace std;
int main()
{
int v()
return 0;
}
I ran it in ideone , and it compiled successfully. 我在ideone中运行它,并且编译成功。 I have the same code in file
test1.cpp
on my computer, I ran g++ test1.cpp
and I got the following error: 我的计算机上的文件
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’
Why dose this happen? 为什么要发生这种情况? is this a bug?
这是一个错误吗? I'm using linux mint, gcc version 4.7.
我正在使用linux mint,gcc版本4.7。
This is commonly known as C++'s most vexing parse. 这通常被称为C ++最烦人的解析。 When you do something like
当你做类似的事情
int f();
the compiler reads this as a function prototype, declaring a function f
that returns an int
. 编译器将此作为函数原型读取,并声明一个返回
int
的函数f
。 If you're using C++11, you should instead do 如果您使用的是C ++ 11,则应改用
int f{}; // f initialized to 0
if you're not using C++11, make sure to initialize the variable right away. 如果您不使用C ++ 11,请确保立即初始化变量。
You are missing a semi-colon here: 您在这里缺少分号:
int v()
^
should be: 应该:
int v() ;
which is a function declaration, not clear that was what was intended though. 这是一个函数声明,虽然不清楚这是什么目的。 If you want to initialize
v
then the following would work: 如果要初始化
v
则可以执行以下操作:
int v(0) ;
or in C++11 : 或在C ++ 11中 :
int v{0} ;
你忘了分号之后
int v();
Ideone is using gcc 4.8.1 for your code (as you can see in your own link) while you are using 4.7 当您使用4.7时,Ideone将gcc 4.8.1用于您的代码(如您在自己的链接中所见)
There are several difference regarding C++ 11 implementation, and apparently it is affected by the line that looks like a function delcaration. 关于C ++ 11的实现,有几个区别 ,显然,它受看起来像函数代用的行的影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.