user569322
user569322

Reputation:

C++ undefined references to functions in namespace

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);
}

Then for the implementation file:

#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.

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?

Upvotes: 4

Views: 7119

Answers (2)

4pie0
4pie0

Reputation: 29724

define it like:

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

otherwise this is just a new function in global namespace

Upvotes: 6

user368361
user368361

Reputation:

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
};

Upvotes: 11

Related Questions