c++ - 如何访问其他 .cc 文件中命名空间中定义的函数
问题描述
我有一个文件名为test.cc
#include <stdio.h>
int doit(){
return 4;
}
namespace abc {
int returnIt(int a){
return a;
}
}
main.cc
我可以使用 doit(),但是如何在不使用 .h 文件的情况下在我的命名空间中使用此函数:
using namespace abc;
int doit();
int main(int argc, const char * argv[]) {
cout<<returnIt(3)<<endl; // print 3
cout<<doit(); // print 4
return 0;
}
解决方案
You can call functions by first declaring them. Example:
namespace abc {
int returnIt(int a); // function declaration
}
int main() {
abc::returnIt(3); // the declared function can now be called
Note that the declaration must be exactly the same as used elsewhere in the program. To achieve identical declarations across translation units, it is conventional to put the declaration into a separate file (called a header) and include that file using the pre-processor whenever the declaration is needed.
推荐阅读
- laravel - Laravel 工厂无法创建,因为已经有一个
- javascript - 如何优化循环
- reactjs - 部署后反应项目错误(最大调用堆栈)
- unicode - Free Pascal 3.2.0 中的 Unicode 编码/解码错误
- python - 禁用均值函数的舍入
- java - 在这种特定情况下,如何阻止 Eclipse 缩进额外的空格?
- python - 如何使用登录的用户名在 Python Django 帖子上添加评论?
- powershell - 在 powershell 中转义 $
- ag-grid - 以编程方式删除所有分组和过滤的简单方法
- linux - while循环比较文件