[英]C++ namespace errors
I'm running mingw-w64 on Windows 7 Pro 64-bit. 我正在Windows 7 Pro 64位上运行mingw-w64。
I'm getting errors when I try the code in a namespace, but it works fine without the namespace. 在命名空间中尝试代码时遇到错误,但是在没有命名空间的情况下可以正常工作。 The code is:
代码是:
//foo.h
#ifndef FOO_H
#define FOO_H
namespace joke {
extern "C" int fooStack[10];
extern "C" int *fooPtr;
extern "C" void fooInit();
extern "C" void fooPush(int iVar);
extern "C" int fooPop();
}
#endif // FOO_H
// foo.cpp
#include "foo.h"
using namespace joke;
int fooStack[10];
int *fooPtr;
void fooInit() {
fooPtr = &fooStack[0] + 9;
}
void fooPush(int iVar) {
*fooPtr = iVar;
fooPtr -= 1;
}
int fooPop() {
int oVar;
fooPtr += 1;
oVar = *fooPtr;
}
// bar.cpp
#include <iostream>
#include "foo.h"
using namespace std;
using namespace joke;
int inVar = 0;
int outVar = 0;
void Report() {
int i;
cout << "Pointer: " << fooPtr << endl;
cout << "Stack: ";
for (i = 0; i < 10; i++) {
cout << fooStack[i] << " ";
}
cout << endl;
cout << "inVar: " << inVar << endl;
cout << "outVar: " << outVar << endl << endl;
}
int main() {
int i;
fooInit();
cout << endl;
cout << "Initializing" << endl;
Report();
for (i = 0; i < 8; i++) {
inVar = (i * 34) + 34;
cout << "set inVar" << endl;
Report();
fooPush(inVar);
cout << "Push inVar" << endl;
Report();
}
for (i = 0; i < 8; i++) {
outVar = fooPop();
cout << "Pop outVar" << endl;
Report();
}
return 0;
}
The makefile: 生成文件:
all:
g++ -S foo.cpp
g++ -S bar.cpp
g++ -c foo.s
g++ -c bar.s
g++ -o FB foo.o bar.o
As I said, this all works fine if the namespace specifications are removed or commented-out. 正如我说的,如果删除或注释掉了命名空间规范,那么一切都可以正常工作。 With the namespace specifications in place, the results are:
使用命名空间规范后,结果为:
c:\work\gccWork\FooTest>make
g++ -S foo.cpp
foo.cpp: In function 'void fooInit()':
foo.cpp:11:2: error: reference to 'fooPtr' is ambiguous
fooPtr = &fooStack[0] + 9;
^
foo.cpp:8:6: note: candidates are: int* fooPtr
int *fooPtr;
^
In file included from foo.cpp:3:0:
foo.h:9:18: note: int* joke::fooPtr
extern "C" int *fooPtr;
^
foo.cpp:11:12: error: reference to 'fooStack' is ambiguous
fooPtr = &fooStack[0] + 9;
^
foo.cpp:7:5: note: candidates are: int fooStack [10]
int fooStack[10];
^
In file included from foo.cpp:3:0:
foo.h:8:17: note: int joke::fooStack [10]
extern "C" int fooStack[10];
^
foo.cpp: In function 'void fooPush(int)':
foo.cpp:15:3: error: reference to 'fooPtr' is ambiguous
*fooPtr = iVar;
^
foo.cpp:8:6: note: candidates are: int* fooPtr
int *fooPtr;
^
In file included from foo.cpp:3:0:
foo.h:9:18: note: int* joke::fooPtr
extern "C" int *fooPtr;
^
foo.cpp:16:2: error: reference to 'fooPtr' is ambiguous
fooPtr -= 1;
^
foo.cpp:8:6: note: candidates are: int* fooPtr
int *fooPtr;
^
In file included from foo.cpp:3:0:
foo.h:9:18: note: int* joke::fooPtr
extern "C" int *fooPtr;
^
foo.cpp: In function 'int fooPop()':
foo.cpp:21:2: error: reference to 'fooPtr' is ambiguous
fooPtr += 1;
^
foo.cpp:8:6: note: candidates are: int* fooPtr
int *fooPtr;
^
In file included from foo.cpp:3:0:
foo.h:9:18: note: int* joke::fooPtr
extern "C" int *fooPtr;
^
foo.cpp:22:10: error: reference to 'fooPtr' is ambiguous
oVar = *fooPtr;
^
foo.cpp:8:6: note: candidates are: int* fooPtr
int *fooPtr;
^
In file included from foo.cpp:3:0:
foo.h:9:18: note: int* joke::fooPtr
extern "C" int *fooPtr;
^
make: *** [all] Error 1
I've looked through various c++ textbooks and references, as well as several references and forums here online. 我浏览了各种C ++教科书和参考资料,以及在线的一些参考资料和论坛。 I don't see any problems with the syntax.
我认为语法没有任何问题。
What am I missing? 我想念什么?
You are asking for C linkage. 您正在要求C链接。 But C has no namespace concept.
但是C没有命名空间概念。 That seems like a conflict of interest to me.
对我而言,这似乎是利益冲突。
fooPtr
is declared both globally (in the unnamed namespace
) and in namespace joke
. fooPtr
在全局(在未namespace
)和namespace joke
。 They are different entities. 他们是不同的实体。
The using namespace joke
then tells the compiler to use names from within joke
as candidates to match identifiers 然后,
using namespace joke
告诉编译器将joke
名称用作候选者以匹配标识符
The names ::fooPtr
and joke::fooPtr
are both visible to the compiler when the code has a statement fooPtr = &fooStack[0] + 9
. 当代码具有语句
fooPtr = &fooStack[0] + 9
时,编译器可以看到名称::fooPtr
和joke::fooPtr
。 The using namespace joke
means that both ::fooPtr
and joke::fooPtr
are valid matches. using namespace joke
意味着::fooPtr
和joke::fooPtr
都是有效的匹配项。 There is no basis to to prefer one over the other, so the compiler rejects the code due to ambiguity. 没有理由偏爱一个,因此编译器由于含糊不清而拒绝了该代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.