我在Windows 7 Pro 64位上运行mingw-w64。
我在命名空间中尝试代码时遇到错误,但没有命名空间就可以正常工作。代码是:
//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;
}
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
正如我所说,如果删除或注释掉命名空间规范,这一切都可以正常工作。使用命名空间规范后,结果如下:
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
我已经在线查看了各种c ++教科书和参考资料,以及一些参考资料和论坛。我没有看到语法有任何问题。
我错过了什么?
答案 0 :(得分:0)
您要求C链接。但是C没有命名空间概念。这对我来说似乎是一种利益冲突。
答案 1 :(得分:0)
fooPtr
)和namespace
中声明了 namespace joke
。它们是不同的实体。
using namespace joke
然后告诉编译器使用joke
中的名称作为匹配标识符的候选者
当代码具有语句::fooPtr
时,编译器可以看到名称joke::fooPtr
和fooPtr = &fooStack[0] + 9
。 using namespace joke
表示::fooPtr
和joke::fooPtr
都是有效匹配。没有理由优先选择其中一个,因此编译器会因模糊而拒绝代码。