mediocrevegetable1
mediocrevegetable1

Reputation: 4217

Linker error: undefined reference to function from header file

I did try to find my specific issue, but the closest I found was this question where the issue was with global variables.

I need make a lexer header file along with it's cpp file to use in main.cpp. here are the two lexer files:

// lexer.hpp
#ifndef LEXER_HPP
#define LEXER_HPP
// Some include files, not really relevant

namespace interpreter
{
    namespace lex
    {
        enum class TokType : char;
        typedef std::vector<std::pair<TokType, std::string>> TokenList;
        typedef std::pair<TokType, std::string> Token;
        TokenList tokenizeString(std::string tokStr);
    } // namespace lex
} // namespace interpreter

#endif
/* --------------------------------------*/
// lexer.cpp
// Again, some not-really-necessary include files
#include "lexer.hpp"
using namespace interpreter::lex;

enum class TokType : char {/* Stuff */};
TokenList tokenizeString(std::string tokStr) {/* Stuff*/};

When I use this stuff in main.cpp and compile, I get the following error:

/usr/bin/ld: /tmp/cc3XtuwW.o: in function `main':
main.cpp:(.text+0x7d): undefined reference to `interpreter::lex::tokenizeString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: error: ld returned 1 exit status

Clearly, something is wrong with my function, but I cannot for the life of me figure out what is actually wrong.

Here's some other information if you need it:

Upvotes: 0

Views: 663

Answers (1)

john
john

Reputation: 88027

Do this

TokenList interpreter::lex::tokenizeString(std::string tokStr)
{
    /* Stuff*/
}

or this

namespace interpreter { namespace lex {
    TokenList tokenizeString(std::string tokStr)
    {
        /* Stuff*/
    }
} }

Although you've said using namespace interpreter::lex; that doesn't mean that newly defined names get added to the interpreter::lex namespace.

Upvotes: 1

Related Questions