I have a Logger class which is expecting a std::string in the following way:
class Logger
{
public:
void Log(const std::string message);
};
And another class is calling it as:
class MyClass
{
public:
void MyMethod()
{
Logger logger;
logger.Log("Hello World!"); // This line is causing linker error.
// std::string myString("Hello World!"); If this and the line below are
// logger.Log(myString); uncommented, I get no errors.
}
};
The error message is:
Logger.cpp: undefined reference to `Logger::Log(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)`
Obviously the linker knows about the Log method, because as I showed above, the commented out lines will allow it to link properly.
Does anyone have an idea what’s going on? Let me know if more information is required.
This is using gcc 4.4.3 under Linux (using Eclipse)
EDIT:
It was my understanding that you could always pass c-style strings to a method expecting an std::string because std::string has an appropriate copy constructor to handle it.
EDITx2:
Facepalm moment. I found the error. Thanks for everyone making me take a harder look at my code. See the complete solution below.
This chunk of code should have been added here in the question:
// cpp file
// This should have been in the header, not the cpp file.
inline void Logger::Log(const std::string message)
{
/* do sutff */
}
Your problem is that you haven’t defined the
Log(akaLogger::Log) method. You’ve declared it, but not defined it anywhere.Another small issue is that you really ought to declare it like this:
There is no particularly good reason to force callers to make a copy of the string they’re passing you.
There are a few possibilities that might explain why you might think it’s defined, even though it isn’t.
First, you may not be linking in the appropriate
.ofile corresponding to the.cppfile were the function is defined. This means you have defined it, but the linker is unaware of your definition.Another possibility is that you’ve defined a function with a similar name that isn’t the same function that’s being called.
Another possibility is that you’ve defined the function to be inline. If the function is defined inline, it’s possible the compiler hasn’t emitted code for it. Many compilers only emit code for an inline function if the definition is available at the time it is referenced, and the manner of reference requires that the function have actual code someplace (such as taking its address).
If you have a function where the definition is inline, that function should generally be in a header file, otherwise the
inlinekeyword is useless, and may even cause you problems since all the rest of your code will assume there is actual code residing at a particular address backing your function declaration, except for the one.cppfile where it appears, which may not reference the function in such a way as to force code to be emitted.