[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


Undefined reference, even though all object files are included

Posted on 2004-10-29
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 13

Accepted Solution

SteH earned 500 total points
ID: 12444960
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

ID: 12444962
>>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

ID: 12458539
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

ID: 12458549
The following link provides an explanatio for the same



Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

650 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