Undefined reference, even though all object files are included

Posted on 2004-10-29
Last Modified: 2013-11-18
I have a template (non-member) function that is declared inside a namespace in an hpp file and defined in a cpp file. It is compiled, and the corresponding o file is sent to the linker OK. My main.cpp includes the relevant hpp file. But I still get an "undefined reference" error to the function when I try to call it from main(). I'm stumped. What could be wrong?
Question by:loveslave
    LVL 13

    Accepted Solution

    Templates need to be declared and defined in the hpp file. The compiler replaces the template arguments to generate actual code it will compile then. So the cpp won't be looked at, when you use the template inside another cpp file.
    LVL 86

    Expert Comment

    >>What could be wrong?

    The problem is that - even though the C++ standard allows separating template declarations and implementations - there's no compiler out there that supports that. So, the easiest workaround is to

    #include "template.cpp"
    LVL 10

    Expert Comment

    As of now, the most widely approach being declaring and defining the templates functions/classes in the header file itself since most compilers do not support separating template declarations from implementation.

    LVL 10

    Expert Comment

    The following link provides an explanatio for the same


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa‚Ķ
    Preface In the first article: A Better Website Login System ( I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now