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 problem here... I have an ASP.NET 3.5 application that has
I'm having strange problems parsing (apparently) correct XML code! The xml parsed is: <?xml
I'm having strange error with installing fixture from dumped data. I am using psycopg2,
Having a strange issue with some C# code - the Getter method for a
I am having a strange DB2 issue when I run DBUnit tests. My DBUnit
I'm having a strange problem. I have to use GetPostBackEventRefence to force a Postback,
I'm having a strange problem in Visual Studio 2008 where my Pending Checkins window
I am having a very strange behavior in JBoss, and I'd like avail myself
Having a heckuva time with this one, though I feel I'm missing something obvious.
I'm having some strange issues building and running a project on another computer. It's

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.