marlon
marlon

Reputation: 7623

Why am I get getting "undefined reference to" error when compiling a c++ program?

I just added one function definition to a .h file and its implementation in its corresponding .cpp file. When I compiled it, I got this compiling error:

./query.so: undefined reference to `Recognizer::skip_one_char(std::string const&, std::string const&)'
collect2: error: ld returned 1 exit status

In Recognizer.h, I added this:

bool skip_one_char(const string &content, const string &tag);

And in Recognizer.cpp, I added this:

bool skip_one_char(const string &content, const string &tag){
     ...
     return false
 }

It seems everything is right, but why do I still this error:

undefined reference 

Also, before I added this additional function implementation to it, the package compiles and runs well.

Upvotes: 0

Views: 54

Answers (1)

Jeremy Friesner
Jeremy Friesner

Reputation: 73041

In Recognizer.cpp you've declared a free-standing function named skip_one_char:

bool skip_one_char(const string &content, const string &tag){
   ...
   return false;
}

... I think what you intended to do was create a method in the Recognizer class with that name, i.e.:

bool Recognizer::skip_one_char(const string &content, const string &tag){
   ...
   return false;
}

... at least, that method is what the linker is looking for and can't find.

Upvotes: 4

Related Questions