C ++未定义对名称空间中函数的引用

时间:2013-04-03 12:46:00

标签: c++ c function namespaces

这一直困扰着我。我有一个命名空间,在该命名空间中我想声明C风格的函数。所以我做了我认为正确的事情:

namespace test
{
    std::deque<unsigned> CSV_TO_DEQUE(const char* data);
    std::deque<unsigned> ZLIB64_TO_DEQUE(const char* data, int width, int height);

    std::string BASE64_DECODE(std::string const& encoded_string);
}

然后是实现文件:

#include "theheaderfile.hpp"

using namespace test;

std::deque<unsigned> CSV_TO_DEQUE(const char* data)
{
     ...
}
std::deque<unsigned> ZLIB64_TO_DEQUE(const char* data, int width, int height)
{
     ...
}

std::string BASE64_DECODE(std::string const& encoded_string)
{
     ...
}

但是,当试图实际调用函数时,我得到一个未定义的引用错误。文件链接,所以我不确定为什么引用是未定义的。

我还应该补充一点,如果我从test命名空间中取出这些函数并将它们保留在全局命名空间中,它们的工作顺利进行。

我想避免在标题中定义函数。这可能吗?

2 个答案:

答案 0 :(得分:10)

using namespace只会导入命名空间以供使用 - 它不允许您定义该命名空间中的函数。

您仍需要在测试命名空间内定义函数:

namespace test {
    // your functions
};

答案 1 :(得分:3)

将其定义为:

std::deque<unsigned> test::CSV_TO_DEQUE(const char* data)
{
     ...
}

否则这只是全局命名空间中的一个新函数