在头文件中添加命名空间函数
Adding namespace functions in header files
我正在尝试使用头文件将单独文件上的命名空间中的函数转移到另一个文件,并且返回错误
">添加"不是"简单"的成员。
我只是想知道是否可以使用前向函数声明,而不是每次在标头中声明函数,据说这会为包含它的每个.cpp不断复制代码。 以下是 1.cpp、2.cpp 和 3.h 的基本大纲。
1.cpp
#include "stdafx.h"
// Check this header file for function declarations.
#include "3.h"
int main()
{
//Example of a single namespace.
std::cout << simple::add(3, 4);
return 0;
}
2.cpp
#include "stdafx.h"
namespace simple {
int simple::add(int x, int y) {
return x + y;
}
}
3.h
#ifndef NAMESPACES
#define NAMESPACES
namespace simple {
int simple::add(int, int);
}
#endif
非常感谢任何回答的人,如果以前有人问过这个问题,我们深表歉意。
解决了这个问题,供此线程的任何未来查看者使用。 您不会像我那样在标头声明或函数本身中使用前面的"simple::"。
更正的代码:
3.h
#ifndef NAMESPACES
#define NAMESPACES
//Header guards are important!
namespace simple {
int add(int, int);
}
#endif
2.cpp
#include "stdafx.h"
#include "3.h"
int simple::add(int x, int y) {
return x + y;
}
不需要对 1.cpp 进行任何更改。
相关文章:
- 在命名空间中定义函数还是限定函数
- CUDA内核和数学函数的显式命名空间
- 在命名空间中使用全局命名空间中的函数
- 是否可以将函数导入命名空间,但不能导出它?
- 使用另一个命名空间中的函数C++
- Visual Studio 无法解决类和命名空间中重载的明确函数
- 命名空间更改函数定义
- C++命名空间中调用 void 函数的错误
- C++ 17 个友元函数声明和内联命名空间
- MFC 函数来自 :: 命名空间?
- 使用类指针重载C++命名空间函数模板专用化替代方法?
- 在头文件中添加命名空间函数
- 命名空间函数的多重定义
- 在多个 cpp 文件中重用未命名的命名空间函数
- 如何正确声明命名空间函数
- 使用 libclang 解析命名空间函数
- C++命名空间函数
- 内联命名空间函数时出错
- 递归std::functional与递归匿名命名空间函数.使用哪一个
- 如何在命名空间函数中使用pragma weak