gpt4 book ai didi

来自不同源文件的c++命名空间

转载 作者:太空狗 更新时间:2023-10-29 20:46:12 24 4
gpt4 key购买 nike

我有一个名为 test1.cpp 的文件

namespace a {
int main1() {
return 3;
}
}

我还有另一个文件test2.cpp

#include <stdio.h>
using a::main1;
int main() {
printf("%d", a::main1());
}

然后我得到一个编译错误,提示 'a' has not been declared with g++。请帮助我找出我在这里错过了什么,以及通常如何做到这一点。

谢谢。

最佳答案

您必须在头文件中声明 namespace 、类和函数,并将其包含在 test2.cpp 文件中。

关于来自不同源文件的c++命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8718010/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com