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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:29:07+00:00 2026-05-18T02:29:07+00:00

I am having a strange linking error. I followed instructions presented here to avoid

  • 0

I am having a strange linking error. I followed instructions presented here to avoid this kind of problems, but I can’t figure out how to split headers and implementation files.

Here’s my test file:

#include <cargo.h>
#include <iostream>
#include <string>

using namespace std;
using namespace dom;

int main()
{
    dom::cargo<string> text("Hello, World!");

    cout << text << endl;

    return 0;
}

Header file for class cargo included in the test:

#ifndef CARGO_H
#define CARGO_H 1

#include "node.h"

#include <iostream>

namespace dom
{
    template <typename Type> class cargo;

    template <typename Type>
    std::ostream& operator<<(std::ostream&, const dom::cargo<Type>&);

    template <typename Type>
    class cargo
        : public dom::node
    {
        Type value;

    public:
        cargo()
            : value()
        { }

        cargo(Type value)
            : value(value)
        { }

        friend std::ostream& operator<< <>(std::ostream&, const dom::cargo<Type>&);
    };
}

#endif // !CARGO_H

And its implementation:

#include "cargo.h"

template <typename Type>
std::ostream& operator<< (std::ostream& ostream, dom::cargo<Type>& cargo)
{
    return (ostream << cargo.value);
}

I’m using CMake to compile and link it all togather.
The linking error I get is about undefined reference to operator <<:

Scanning dependencies of target test
[100%] Building CXX object Tests/CMakeFiles/test.dir/test0.c++.o
Linking CXX executable test
CMakeFiles/test.dir/test0.c++.o: In function `main':
test0.c++:(.text+0x9b): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& dom::operator<< <std::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::basic_ostream<char, std::char_traits<char> >&, dom::cargo<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)'
collect2: ld returned 1 exit status
make[2]: *** [Tests/test] Error 1
make[1]: *** [Tests/CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2

What am I doing wrong? Please help me!

  • 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-18T02:29:07+00:00Added an answer on May 18, 2026 at 2:29 am

    (Member) function templates are not functions; the linker doesn’t see them unless you instantiate them. Source files are compiled separately, so if you put a (member) function template in one source file and don’t explicitly instantiate it, the linker won’t see it.

    So in your case, the function template has not been turned into a function in cargo.o, so the linker reports an error because main.o depends on it. You need to put the template in the header file, or explicitly instantiate it in cargo.cpp.

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

Sidebar

Related Questions

I'm having a strange issue with counting form elements in jQuery. While I can
I'm having a strange issue with my modal view. The modal view asks for
I am having a strange behaviour in VB 2005. I hit F5 to run
I am having an strange problem in visual studio 2010, visual basic, and using
I am having a strange problem. There are two ajax calls in the code.
I am having a strange problem in my code. I have many asserts scattered
I'm having a strange issue that I haven't encountered before. I'm working on the
I was able to build one application up to this morning, then I don't
First of all, sorry if this might be a stupid question. I'm very new

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.