在 C++ 中将包含命名空间定义的一个文件与使用该命名空间的另一个文件链接时出现问题?

发布于 2024-10-17 17:36:38 字数 1133 浏览 4 评论 0原文

我有两个文件,一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小…楫夜泊 2024-10-24 17:36:38

ani.cpp 中,在执行 using 之前,您从未告诉编译器程序中的其他位置有一个命名空间 Anirudh。如果您习惯了其他模块系统,这可能看起来很奇怪。

您可以做的是在调用名称空间+函数之前声明它,并在 ani.cpp 中的 using namespace 之前声明这些行,

namespace Anirudh{    
    void start();
}

通常这些声明会包含在标头中但这对于这个简单的例子来说可能是不需要的。

Within ani.cpp you never told the compiler that there's a namespace Anirudh somewhere else in the program prior to doing the using. 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 in ani.cpp

namespace Anirudh{    
    void start();
}

Often these declarations would be wrapped up in a header but that's probably not needed for this simple example.

浪推晚风 2024-10-24 17:36:38

怎么样对函数进行原型设计:

namespace Anirudh {
    void start();
} // namespace Anirudh

int main(...){
//...

What about prototyping the function a la:

namespace Anirudh {
    void start();
} // namespace Anirudh

int main(...){
//...
一枫情书 2024-10-24 17:36:38

如果您没有头文件,那么您至少需要做的是:在调用函数之前在 ani.cpp 中编写函数的原型,

using namespace Anirudh;

void Anirudh::start();

int main(){

    start();
    return 0;
}

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,

using namespace Anirudh;

void Anirudh::start();

int main(){

    start();
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文