我有一个包含一组函数的文件。对于其中一个函数,我想编写一个辅助函数,它基本上采用 char * 并跳过所有空格。
我认为应该这样做:
namespace {
const int kNotFound = -1;
void SkipWhitespace(const char *s); // forward declaration - doesn't seem to work?
}
void foo(const char *s1, const char *s2) {
// do some stuff
SkipWhitespace(s1);
SkipWhitespace(s2);
// continue with other stuff
}
void SkipWhitespace(const char *s) {
for (; !isspace(s); ++s) {}
}
但这给了我一个编译器错误。我是否需要将定义放在未命名的命名空间中?
最佳答案
您还必须在匿名命名空间中定义它:
namespace {
...
void SkipWhitespace(const char *s); // forward declaration - doesn't seem to work?
}
void foo(const char *s1, const char *s2) {
...
}
namespace {
void SkipWhitespace(const char s*) {
for (; !isspace(s); ++s) {}
}
}
但除非存在循环依赖,否则我不确定这有什么值(value)。只需一次性声明和定义函数。
关于c++ - 是否可以将函数声明放在未命名的命名空间中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4428589/