-1

I've been banging my head against the wall for the past while trying to figure this out. I want to use this function that I got from stack overflow with these function parameters:

void split(const string& str, const string& delimiters, vector<string>& tokens);

I define it in the header file of my ServerHandler class (ServerHandler.h):

public:
   ServerHandler();
   ~ServerHandler();
   void split(const string& str, const string& delimiters, vector<string>& tokens);
   void handleRequest(int client, string request);
   void handlePutRequest(string request);
   bool isValidPutRequest(vector<string> requestTokens);

I put it in my ServerHandler.cc class:

void split(const string& str, const string& delimiters, vector<string>& tokens)
{
...
}

Then I try to call it from my method where I define the two strings and the vector object:

vector<string> requestTokens;
...
string firstHalf;
firstHalf = "tell mike hello!";
...
string spaceDelimiter;
spaceDelimiter = " ";
...
split(firstHalf, spaceDelimiter, requestTokens);  // Throws compiler  error here

How do I fix this compiler error?

EDIT:

This is the compiler error:

ServerHandler.cc:(.text+0x674): undefined reference to `ServerHandler::split(std::string const&, std::string const&, std::vector<std::string, std::allocator<std::string> >&)'
idungotnosn
  • 2,001
  • 4
  • 29
  • 36

2 Answers2

4

Add the class name to the function definition:

void ServerHandler::split(const string& str,
                          const string& delimiters,
                          vector<string>& tokens)
{
...
}
AlexD
  • 32,156
  • 3
  • 71
  • 65
  • This is the right answer. I'll mark it as correct in 12 minutes. Gosh, what a total derp on my part. All of my other methods have ServerHandler:: prepended correctly too. – idungotnosn Sep 09 '14 at 00:51
  • @idungotnosn Finding a dupe was faster! You may consider to leave your question, to help others find the more canonical Q&A faster. – πάντα ῥεῖ Sep 09 '14 at 01:01
2

You are not declaring the scope of the method in your implementation file:

void ServerHandler::split(const string& str, const string& delimiters, vector<string>& tokens)
     ^^^^^^^^^^^^^^^

That's not a compiler error though, it's a linker error. The compiler goes fine because you declared the method inside the header file, then during linking phase that method is not resolved.

Jack
  • 131,802
  • 30
  • 241
  • 343