【发布时间】:2014-03-17 17:50:09
【问题描述】:
简单,请看这段代码:
namespace B
{
struct A{ int i; } ;
A getA(int i);
}
// ____ if I'll delete '::' then program successfull compiled.
// /
::B::A ::B::getA(int i){ ::B::A a = {i}; return a;}
#include <cstdio>
int main()
{
::B::A a = ::B::getA(2);
printf("%d\n", a.i);
}
VS2010错误列表:
1>main.cpp(94): error C3083: 'B': the symbol to the left of a '::' must be a type
1>main.cpp(94): error C2039: 'getA' : is not a member of 'B::A'
1>main.cpp(88) : see declaration of 'B::A'
1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>main.cpp(94): error C2440: 'return' : cannot convert from 'B::A' to 'int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>main.cpp(94): error C2617: 'getA' : inconsistent return statement
1>main.cpp(94) : see declaration of 'getA'
Gcc.4.8.1 错误列表(from ideone.com):
prog.cpp:10:1: error: ‘B’ in ‘struct B::A’ does not name a type
::B::A ::B::getA(int i){ ::B::A a = {i}; return a;}
^
问:这是一个错误还是我不明白什么?
【问题讨论】:
-
据我所知,“::”之前没有任何限定符意味着“在全局命名空间中找到它”。所以对于可能认为它是一个类的编译器来说,这是相当误导的。不知道为什么
-
@DavidKernin:因为前面的空格没有意义,所以附加到前面的
A。 -
@MikeSeymour:我完全忽略了这一点。好眼力!
-
不知道,下次真的要记住了
标签: c++ visual-studio-2010 gcc