3

Possible Duplicate:
Why do I get “unresolved external symbol” errors when using templates?

I am making a linkedList. I am using an external iterator. The Iterator class is a template, and I am implementing my methods in the Iterator.h.

Here is the template:

#pragma once

#include "Node.h"

 namespace list_1
 {

template<typename T>
class Iterator
{
public:
    Iterator<T> (Node<T> *np);
    void operator++();
    bool is_item();
    T operator* ();

private:
    Node<T>* n;
};

template<typename T>
Iterator<T>::Iterator (Node<T> *np)
{

}

template<typename T>
void Iterator<T>::operator++()
{

}

template<typename T>
bool Iterator<T>::is_item()
{
    return false;
}

template<typename T>
T Iterator<T>::operator* ()
{

}
 }

I get this error message when I try to compile: 1>list_test.obj : error LNK2019: unresolved external symbol "public: void __thiscall list_1::Iterator<double>::operator++(void)"

Plus about seven other similar errors in the whole project.

Am I doing something wrong here? Or is it something else I am doing wrong?

Thanks!

Community
  • 1
  • 1
SirRupertIII
  • 12,324
  • 20
  • 72
  • 121
  • Is `list_1` a class or namespace? Is the `Iterator` class definition inside or outside the `list_1` definiton? Is the `operator++` definition inside or outside the `list_1` definition? – aschepler Jan 25 '13 at 02:29
  • 1
    I don't see an `inline` in front of that `operator` implementation, so i clearly isn't in the same **header file** as the template declaration. Unless this is all in a *single* source file, you're not going to get what you want without explicit instantiation. – WhozCraig Jan 25 '13 at 02:29
  • Sorry, Iterator is in the namespace list_1. `template void Iterator::operator++()` is also in list_1. – SirRupertIII Jan 25 '13 at 02:31
  • 2
    @WhozCraig: `inline` doesn't do much at all on a template function or member function of a class template. – aschepler Jan 25 '13 at 02:31
  • @aschepler you're correct, but for non-template code it is the thing that raises duplicate-references, and thus I always use it on any out-of-class member impl, even template ones. You are most-correct, however. – WhozCraig Jan 25 '13 at 02:35
  • why is operator++ returning void? – Chubsdad Jan 25 '13 at 02:53
  • I can't repro this when the file mentioned that contains all of this is a *header file*, and lord knows [i've tried.](http://ideone.com/Xvs6rg). Clearly I'm missing something. – WhozCraig Jan 25 '13 at 03:00
  • Maybe try a `make clean` or `Rebuild All` option. – aschepler Jan 25 '13 at 03:11
  • The code you have shown us is correct. You'll get a correct answer if you provide complete code that shows your error. – Drew Dormann Jan 25 '13 at 03:26
  • My code is in the .h not the .cpp. This question is not similar to the "duplicate" question. The titles are similar, but the idea is completely different. – SirRupertIII Jan 25 '13 at 23:10

1 Answers1

0

If I read your error message correctly, you Iterator takes Node<T> as input, however you are applying double to it which is not applicable. To support non-Node<T> type, you need to specialize Iterator<T>.

public: void __thiscall list_1::Iterator<double>::operator++(void)"
                                         ^^^^^
billz
  • 44,644
  • 9
  • 83
  • 100