0
votes

I am working on homework where I have to write a template and then implement the template into a driver. Unfortunately, I'm receiving an error when compiling. I'm just beginning to learn the more complex aspects of C++, and I have no idea how to resolve this problem.

/tmp/ccdvvLpF.o:main.cpp: (.text$_ZN11LinkedQueueISsE7enqueueERKSs[LinkedQueue, std::allocator > >::enqueue(std::basic_string, std::allocator > const&)]+0x4a): undefined reference to LinkedQueue<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::number' /tmp/ccdvvLpF.o:main.cpp:(.text$_ZN11LinkedQueueISsE7enqueueERKSs[LinkedQueue<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::enqueue(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)]+0x66): undefined reference toLinkedQueue, std::allocator > >::number' /usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/bin/ld: /tmp/ccdvvLpF.o: bad reloc address 0x66 in section `.text$_ZN11LinkedQueueISsE7enqueueERKSs[LinkedQueue, std::allocator > >::enqueue(std::basic_string, std::allocator > const&)]' collect2: ld returned 1 exit status

Thanks.

1
The homework tag is deprecated.chris
Your header guard ends prematurely. Is that intended?pmr

1 Answers

3
votes

static int number is never defined. Add the following definition outside of your class.

template <class T> int LinkedQueue<T>::number = 0;

static members are special, static int number inside the class is only a declaration and not a definition. The reason for this is because headers are usually included in multiple translation units, so C++ forces you to define them outside of the class usually in a cpp file so that you do not get multiple definition errors.