Witam, jestem w trakcie implementacji listy dwukierunkowej, i napotkałem błąd.
Podzieliłem listę na plik nagłówkowy i .cpp, i mam jakieś błędy prawdopodobnie przez użycie szablonu.
.h
#ifndef DOUBLYLINKEDLIST_H
#define DOUBLYLINKEDLIST_H
template<typename type>
struct Iterator
{
type item;
Iterator * next;
Iterator * prev;
};
template<typename type>
class DoublyLinkedList
{
private:
unsigned int size;
Iterator<type> * first;
Iterator<type> * last;
public:
int getSize() { return size; }
bool Contains(type item);
Iterator<type> * getFirst() { return first; }
Iterator<type> * getLast() { return last; }
Iterator<type> * GetIteratorAt(int index);
void PopBack() { RemoveAt(last); }
void PopFront() { RemoveAt(first); }
void InsertAfter(Iterator<type> * it, type item);
void InsertBefore(Iterator<type> * it, type item);
void Remove(type item);
void RemoveAt(Iterator<type> * it);
void PushBack(type item);
void PushFront(type item);
DoublyLinkedList(type item);
};
#endif
.cpp
#include "stdafx.h"
#include "DoublyLinkedList.h"
#include <iostream>
#include <windows.h>
using namespace std;
template<typename type>
inline bool DoublyLinkedList<type>::Contains(type item)
{
Iterator<type> * it = DoublyLinkedList::first;
for (int i = 0; i<size; i++, it = it->next)
if (it->item == item)
return true;
return false;
}
template<typename type>
inline Iterator<type> * DoublyLinkedList<type>::GetIteratorAt(int index)
{
if (index >= size)
{
//throw new out_of_range;
//throw std::runtime_error("My very own message");
system("pause > nul");
cout << "Podano nieprawidlowy indeks. Zakres <0 ; getSize()-1>\n";
return NULL;
}
Iterator<type> * it = first;
for (int i = 0; i<size; i++, it = it->next)
if (i == index)
return it;
}
template<typename type>
inline void DoublyLinkedList<type>::InsertAfter(Iterator<type> * it, type item)
{
Iterator<type> * newIterator = new Iterator<type>;
newIterator->prev = it; //poprzedni dla nowego = it
if (it->next != it) //nastepny dla it != it
{
it->next->prev = newIterator; //poprzedni nastepnego dla it = nowy
newIterator->next = it->next; //nastepny dla nowego to nastepny it
}
it->next = newIterator; //nastepny dla it = nowy
newIterator->item = item;
if (it == last)
last = newIterator;
size++;
}
template<typename type>
inline void DoublyLinkedList<type>::InsertBefore(Iterator<type> * it, type item)
{
Iterator<type> * newIterator = new Iterator<type>;
newIterator->next = it;
if (it->prev != it)
{
it->prev->next = newIterator;
newIterator->prev = it->prev;
}
it->prev = newIterator;
newIterator->item = item;
if (it == first)
first = newIterator;
size++;
}
template<typename type>
inline void DoublyLinkedList<type>::Remove(type item)
{
Iterator<type> * it = first;
for (int i = 0; i<size; i++, it = it->next)
{
if (it->item == item)
{
RemoveAt(it);
return;
}
}
}
template<typename type>
inline void DoublyLinkedList<type>::RemoveAt(Iterator<type> * it)
{
it->prev->next = it->next;
it->next->prev = it->prev;
if (size > 1)
{
if (it == first) //set new first
first = it->next;
if (it == last) //set new last
last = it->prev;
}
delete(it); //release memory
size--;
}
template<typename type>
inline void DoublyLinkedList<type>::PushBack(type item)
{
Iterator<type> * newIterator = new Iterator<type>;
newIterator->next = first;
newIterator->prev = last;
last->next = newIterator;
first->prev = newIterator;
last = newIterator;
newIterator->item = item;
size++;
}
template<typename type>
inline void DoublyLinkedList<type>::PushFront(type item)
{
Iterator<type> * newIterator = new Iterator<type>;
newIterator->next = first;
newIterator->prev = last;
last->next = newIterator;
first->prev = newIterator;
first = newIterator;
newIterator->item = item;
size++;
}
template<typename type>
inline DoublyLinkedList<type>::DoublyLinkedList(type item)
{
Iterator<type> * it = new Iterator<type>;
it->prev = it;
it->next = it;
it->item = item;
last = it;
first = it;
size = 1;
}
Wcześniej zrobilem także deklarację szablonów w pliku .h, dla każdej metody je zawierającej, ale również były błędy.
Błędy:
Severity Code Description Project File Line Source Suppression State
Error LNK1120 9 unresolved externals DoublyLinkedList D:\visual Studio projects\Nauka C++\DoublyLinkedList\Debug\DoublyLinkedList.exe 1 Build
Severity Code Description Project File Line Source Suppression State
Error LNK2019 unresolved external symbol "public: bool __thiscall DoublyLinkedList<int>::Contains(int)" (?Contains@?$DoublyLinkedList@H@@QAE_NH@Z) referenced in function "void __cdecl RemoveByValue(void)" (?RemoveByValue@@YAXXZ) DoublyLinkedList D:\visual Studio projects\Nauka C++\DoublyLinkedList\DoublyLinkedList\Main.obj 1 Build
Severity Code Description Project File Line Source Suppression State
Error LNK2019 unresolved external symbol "public: struct Iterator<int> * __thiscall DoublyLinkedList<int>::GetIteratorAt(int)" (?GetIteratorAt@?$DoublyLinkedList@H@@QAEPAU?$Iterator@H@@H@Z) referenced in function "void __cdecl InsertAt(void)" (?InsertAt@@YAXXZ) DoublyLinkedList D:\visual Studio projects\Nauka C++\DoublyLinkedList\DoublyLinkedList\Main.obj 1 Build
Severity Code Description Project File Line Source Suppression State
Error LNK2019 unresolved external symbol "public: void __thiscall DoublyLinkedList<int>::InsertAfter(struct Iterator<int> *,int)" (?InsertAfter@?$DoublyLinkedList@H@@QAEXPAU?$Iterator@H@@H@Z) referenced in function "void __cdecl InsertAt(void)" (?InsertAt@@YAXXZ) DoublyLinkedList D:\visual Studio projects\Nauka C++\DoublyLinkedList\DoublyLinkedList\Main.obj 1 Build
Severity Code Description Project File Line Source Suppression State
Error LNK2019 unresolved external symbol "public: void __thiscall DoublyLinkedList<int>::InsertBefore(struct Iterator<int> *,int)" (?InsertBefore@?$DoublyLinkedList@H@@QAEXPAU?$Iterator@H@@H@Z) referenced in function "void __cdecl InsertAt(void)" (?InsertAt@@YAXXZ) DoublyLinkedList D:\visual Studio projects\Nauka C++\DoublyLinkedList\DoublyLinkedList\Main.obj 1 Build
Severity Code Description Project File Line Source Suppression State
Error LNK2019 unresolved external symbol "public: void __thiscall DoublyLinkedList<int>::PushBack(int)" (?PushBack@?$DoublyLinkedList@H@@QAEXH@Z) referenced in function "void __cdecl PushBack(void)" (?PushBack@@YAXXZ) DoublyLinkedList D:\visual Studio projects\Nauka C++\DoublyLinkedList\DoublyLinkedList\Main.obj 1 Build
Severity Code Description Project File Line Source Suppression State
Error LNK2019 unresolved external symbol "public: void __thiscall DoublyLinkedList<int>::PushFront(int)" (?PushFront@?$DoublyLinkedList@H@@QAEXH@Z) referenced in function "void __cdecl PushFront(void)" (?PushFront@@YAXXZ) DoublyLinkedList D:\visual Studio projects\Nauka C++\DoublyLinkedList\DoublyLinkedList\Main.obj 1 Build
Severity Code Description Project File Line Source Suppression State
Error LNK2019 unresolved external symbol "public: void __thiscall DoublyLinkedList<int>::Remove(int)" (?Remove@?$DoublyLinkedList@H@@QAEXH@Z) referenced in function "void __cdecl RemoveByValue(void)" (?RemoveByValue@@YAXXZ) DoublyLinkedList D:\visual Studio projects\Nauka C++\DoublyLinkedList\DoublyLinkedList\Main.obj 1 Build
Severity Code Description Project File Line Source Suppression State
Error LNK2019 unresolved external symbol "public: void __thiscall DoublyLinkedList<int>::RemoveAt(struct Iterator<int> *)" (?RemoveAt@?$DoublyLinkedList@H@@QAEXPAU?$Iterator@H@@@Z) referenced in function "public: void __thiscall DoublyLinkedList<int>::PopBack(void)" (?PopBack@?$DoublyLinkedList@H@@QAEXXZ) DoublyLinkedList D:\visual Studio projects\Nauka C++\DoublyLinkedList\DoublyLinkedList\Main.obj 1 Build
Severity Code Description Project File Line Source Suppression State
Error LNK2019 unresolved external symbol "public: __thiscall DoublyLinkedList<int>::DoublyLinkedList<int>(int)" (??0?$DoublyLinkedList@H@@QAE@H@Z) referenced in function "void __cdecl `dynamic initializer for 'list''(void)" (??__Elist@@YAXXZ) DoublyLinkedList D:\visual Studio projects\Nauka C++\DoublyLinkedList\DoublyLinkedList\Main.obj 1 Build
Dziękuję za pomoc.