在头文件中添加命名空间函数

Adding namespace functions in header files

本文关键字:命名空间 函数 添加 文件      更新时间:2023-10-16

我正在尝试使用头文件将单独文件上的命名空间中的函数转移到另一个文件,并且返回错误

">

添加"不是"简单"的成员。

我只是想知道是否可以使用前向函数声明,而不是每次在标头中声明函数,据说这会为包含它的每个.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 进行任何更改。