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 838103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:14:57+00:00 2026-05-15T05:14:57+00:00

I have a linker error I’ve reduced to a simple example. The build output

  • 0

I have a linker error I’ve reduced to a simple example.
The build output is:

debug/main.o: In function main':
C:\Users\Dani\Documents\Projects\Test1/main.cpp:5:
undefined reference to
log&
log::operator<< (char
const (&) [6])’
collect2: ld returned
1 exit status

It looks like the linker ignores the definition in log.cpp.
I also cant put the definition in log.h because I include the file alot of times and it complains about redefinitions.

main.cpp:

#include "log.h"

int main()
{
    log() << "hello";
    return 0;
}

log.h:

#ifndef LOG_H
#define LOG_H

class log
{
public:
    log();
    template<typename T>
    log &operator <<(T &t);
};

#endif // LOG_H

log.cpp:

#include "log.h"
#include <iostream>

log::log()
{
}

template<typename T>
log &log::operator <<(T &t)
{
    std::cout << t << std::endl;
    return *this;
}
  • 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-15T05:14:58+00:00Added an answer on May 15, 2026 at 5:14 am

    I guess this is your first use of templates, so I’ll try to be didactic.

    You can think of template as some kind of type-aware macros. This type awareness is of course not to be neglected, since it grants type safety for free. This does mean however than template functions or classes are NOT functions or classes: they are model that will be used to generate functions or classes.

    For example:

    template <class T>
    void foo(T t) { std::cout << t << "\n"; }
    

    This is a template function, it allows me to define something once and apply it to many different types.

    int i;
    foo(i); // [1]
    

    This causes the instantiation of the template. Basically, it means that a function is created according to the model, but replacing all occurrences of T by int.

    double d;
    foo(d);    // Another instantiation, this time with `T` replaced by `double`
    foo(d);    // foo<double>() already exists, it's reused
    

    Now, this idea of model is very important. If the definition of the model is not present in the header file, then the compiler does not know how to define the method.

    So, you have 2 solutions here:

    1. Define it in the header
    2. Explicitly instantiate it

    The 2 have different uses.

    (1) is the classic way. It’s easier because you don’t restrict the user to a subset of the types. However it does mean that the user depends on the implementation (change it, she recompiles, and you need to pull the dependencies in the header)

    (2) is less often used. For full compliance with the standard it requires:

    • That you declare the specialization in the header (template <> void foo<int>();) so as to let the compiler know it exists
    • That you fully define it in one of the translation units linked

    The main advantage is that, like classic functions, you isolate the client from the implementation.

    gcc is quite lenient because you can forgo the declaration and it should work.

    I should also note that it is possible to define a method twice, with different implementations. This is of course an error, as it is in direct violation of the ODR: One Definition Rule. However most linkers don’t report it because it’s quite common to have one implementation per object, and they just pick the first and assume the others will be equivalent (it’s a special rule for templates). So if you do want to use explicit instantiation, take great care to only define it once.

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

Sidebar

Ask A Question

Stats

  • Questions 451k
  • Answers 451k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In core-data (not writting exact syntax) follow these steps: fetch… May 15, 2026 at 8:56 pm
  • Editorial Team
    Editorial Team added an answer Try the following, this is an updated version of your… May 15, 2026 at 8:56 pm
  • Editorial Team
    Editorial Team added an answer Looks like it should be model instance (which comment is)… May 15, 2026 at 8:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.