Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4117158
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:47:59+00:00 2026-05-20T22:47:59+00:00

I have a Logger class which is expecting a std::string in the following way:

  • 0

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 */
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T22:48:00+00:00Added an answer on May 20, 2026 at 10:48 pm

    Your problem is that you haven’t defined the Log (aka Logger::Log) method. You’ve declared it, but not defined it anywhere.

    Another small issue is that you really ought to declare it like this:

    void Log(const ::std::string &message);
    

    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 .o file corresponding to the .cpp file 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 inline keyword 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 .cpp file where it appears, which may not reference the function in such a way as to force code to be emitted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a logger system which basically is a fancy way of writing my
I have a Logger class which is implemented singleton. The class is simple and
I have a class which I can write like this: class FileNameLoader { public:
I have created my own simple logger class which uses COM interop to redirect
I have a class (for logging) which derives from std::ostream. Stripped down, it looks
I have the following code which lets me execute a workflow. This could be
I have a class which initializes my log4j. This code will never print or
I have this Class which response to perform to be run by "Resque", I
I have a logging class which has operator<< overloaded. So I can do things
We have lots of logging calls in our app. Our logger takes a System.Type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.