• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Deklaracja metod klas zawierających szablony

VPS Starter Arubacloud
–1 głos
288 wizyt
pytanie zadane 12 stycznia 2017 w C i C++ przez timrh Mądrala (6,030 p.)

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.

komentarz 12 stycznia 2017 przez timrh Mądrala (6,030 p.)
upp

1 odpowiedź

–1 głos
odpowiedź 12 stycznia 2017 przez unknown Nałogowiec (39,560 p.)
wybrane 13 stycznia 2017 przez timrh
 
Najlepsza
Przenieś definicje szablonu do pliku nagłówkowego
komentarz 12 stycznia 2017 przez timrh Mądrala (6,030 p.)
Przenieść w sensie usunąć z .cpp i dać w .h?
komentarz 12 stycznia 2017 przez unknown Nałogowiec (39,560 p.)
Tak
komentarz 12 stycznia 2017 przez timrh Mądrala (6,030 p.)
Do każdej metody korzystającej z szablonu type wywala teraz błąd niezdefiniowanego identyfikatora.
komentarz 13 stycznia 2017 przez unknown Nałogowiec (39,560 p.)
Pokaż kod i dokładne errory, bo mi w VS 2017 po przeniesieniu ciał metod do pliku *.h wszystko działa.
komentarz 13 stycznia 2017 przez timrh Mądrala (6,030 p.)
Wiem że tak to będzie działać, ale jak widzisz ja chcę to mieć w dwóch plikach.
komentarz 13 stycznia 2017 przez unknown Nałogowiec (39,560 p.)
komentarz 13 stycznia 2017 przez timrh Mądrala (6,030 p.)
Do tego czasu już sam się tego dowiedziałem, ale i tak dziękuje.

Podobne pytania

0 głosów
1 odpowiedź 751 wizyt
pytanie zadane 8 grudnia 2017 w C i C++ przez Jakub 0 Pasjonat (23,120 p.)
0 głosów
1 odpowiedź 382 wizyt
pytanie zadane 14 lutego 2019 w C i C++ przez Sic Dyskutant (8,510 p.)
0 głosów
3 odpowiedzi 252 wizyt
pytanie zadane 15 lutego 2016 w C i C++ przez Exus Początkujący (420 p.)

92,453 zapytań

141,262 odpowiedzi

319,085 komentarzy

61,854 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Akademia Sekuraka 2024 zapewnia dostęp do minimum 15 szkoleń online z bezpieczeństwa IT oraz dostęp także do materiałów z edycji Sekurak Academy z roku 2023!

Przy zakupie możecie skorzystać z kodu: pasja-akademia - użyjcie go w koszyku, a uzyskacie rabat -30% na bilety w wersji "Standard"! Więcej informacji na temat akademii 2024 znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...