Undefined symbols for x86_64 architecture
I have this error:
Undefined symbols for architecture x86_64:
"my::Queue<int>::Queue()", referenced from:
_main in ccdwI88X.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
for this code 'main.cpp':
#include "Queue.hpp"
int main()
{
my::Queue<int> myqueue;
return 0;
}
'Queue.hpp':
#ifndef QUEUE_HH__
#define QUEUE_HH__
namespace my
{
template <typename T>
class Queue
{
public:
Queue();
};
}
#endif
and 'Queue.cpp':
#include "Queue.hpp"
template <typename T>
my::Queue<T>::Queue()
{
}
The answer is here: fooobar.com/questions/851251 / ... - this is what I think you need.
If I edit the file Queue.cppfor this:
#include "Queue.hpp"
template <typename T>
my::Queue<T>::Queue()
{
}
template class my::Queue<int>;
.. it compiles fine.
Detailed explanation, please refer to the above address.
The easiest and safest way to use templates is to always include the definition (implementation) of class functions inside the file .hpp, and not in a separate file .cpp.
: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12