在 C++ 中将包含命名空间定义的一个文件与使用该命名空间的另一个文件链接时出现问题?
我有两个文件,一个名为 test.cpp,另一个名为 ani.cpp。
test.cpp 如下:
#include<iostream>
namespace Anirudh{
void start(){
std::cout<<"This is the start function of the namespace Anirudh\n";
}
}
文件 ani.cpp 如下
#include<iostream>
using namespace Anirudh;
int main(){
start();
return 0;
}
这就是我在终端上所做的
anirudh@anirudh-Aspire-5920:~/Desktop/testing$ g++ -c test.cpp
anirudh@anirudh-Aspire-5920:~/Desktop/testing$ g++ test.o ani.cpp
ani.cpp:3: error: ‘Anirudh’ is not a namespace-name
ani.cpp:3: error: expected namespace-name before ‘;’ token
ani.cpp: In function ‘int main()’:
ani.cpp:6: error: ‘start’ was not declared in this scope
anirudh@anirudh-Aspire-5920:~/Desktop/testing$
这是我第一次尝试在 C++ 中定义自己的命名空间并在另一个代码中使用它。我在 ani.cpp
文件中的 #include "test.cpp"
之后运行了代码,但我想链接 test.cpp
的目标代码code> 与 ani.cpp
而不是将其包含在 ani.cpp 中 我什至尝试过 extern 命名空间 Anirudh;但这没有用。当然,有一种正确的方法来链接它们,但我现在还不知道。所以请赐教。提前致谢。
I have two files once is named as test.cpp and another as ani.cpp.
test.cpp is as follows:
#include<iostream>
namespace Anirudh{
void start(){
std::cout<<"This is the start function of the namespace Anirudh\n";
}
}
and the file ani.cpp is as follows
#include<iostream>
using namespace Anirudh;
int main(){
start();
return 0;
}
and this is what I am doing on the terminal
anirudh@anirudh-Aspire-5920:~/Desktop/testing$ g++ -c test.cpp
anirudh@anirudh-Aspire-5920:~/Desktop/testing$ g++ test.o ani.cpp
ani.cpp:3: error: ‘Anirudh’ is not a namespace-name
ani.cpp:3: error: expected namespace-name before ‘;’ token
ani.cpp: In function ‘int main()’:
ani.cpp:6: error: ‘start’ was not declared in this scope
anirudh@anirudh-Aspire-5920:~/Desktop/testing$
This is the first time I am trying to define my own namespace in C++ and using it in another code. I got my code running after #include "test.cpp"
in my ani.cpp
file but I want to link the object code of test.cpp
with ani.cpp
rather than including it inside ani.cpp
I have even tried extern namespace Anirudh; but that didn't work. Of-course there is a proper way to link them which I do not know right now. So please enlighten me. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
ani.cpp
中,在执行using
之前,您从未告诉编译器程序中的其他位置有一个命名空间 Anirudh
。如果您习惯了其他模块系统,这可能看起来很奇怪。您可以做的是在调用名称空间+函数之前声明它,并在
ani.cpp
中的using namespace
之前声明这些行,通常这些声明会包含在标头中但这对于这个简单的例子来说可能是不需要的。
Within
ani.cpp
you never told the compiler that there's anamespace Anirudh
somewhere else in the program prior to doing theusing
. If you're used to other module systems this probably seems quirky.What you can do is declare the namespace+function prior to calling it, with these lines before the
using namespace
inani.cpp
Often these declarations would be wrapped up in a header but that's probably not needed for this simple example.
怎么样对函数进行原型设计:
What about prototyping the function a la:
如果您没有头文件,那么您至少需要做的是:在调用函数之前在
ani.cpp
中编写函数的原型,If you don't have header file, then the least you've to do is : write the prototype of your function in
ani.cpp
before calling it as,