C ++对命名空间中函数的未定义引用 [英] C++ undefined references to functions in namespace

查看:114
本文介绍了C ++对命名空间中函数的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这困扰了我一段时间.我有一个名称空间,在该名称空间中我想声明C样式的函数.所以我做了我认为正确的事情:

This has been bothering me for awhile. I have a namespace, and in that namespace I want to declare C-style functions. So I did what I thought was right:

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)
{
     ...
}

但是,当尝试实际调用函数时,出现未定义的引用错误.该文件链接,所以我不确定为什么未定义引用.

However, when trying to actually call the functions, I get an undefined reference error. The file links, so I'm not sure why the references are undefined.

我还应该补充一点,如果我从 test 命名空间中取出这些函数并将它们保留在全局命名空间中,那么它们将毫无障碍地工作.

I should also add that if I take the functions out of the test namespace and leave them in the global namespace, they work without a hitch.

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

I want to avoid defining the functions within the header. Is this possible?

推荐答案

使用名称空间将仅导入要使用的名称空间-不允许您在该名称空间中定义函数.

using namespace will only import the namespace for use - it won't let you define the functions in that namespace.

您仍然需要在测试名称空间中定义函数:

You still need to define the functions inside the test namespace:

namespace test {
    // your functions
};

这篇关于C ++对命名空间中函数的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆