error unresolved external symbol

hi all i have big problem can you help me please , i have a dynamic list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  //ordlist.h
#ifndef ORDLIST_H
#define ORDLIST_H
#include<iostream>
using namespace std;
template <class T>
class ordlist
{
public:
	ordlist(int size1);
	~ordlist();
	void insert(const ordlist& insertitem);
	void remove(const ordlist& removeitem);
	void removeat(int location);
	int search(const ordlist& item);
	bool isEmpty();
	bool isFull();
	ordlist(const ordlist<T>&);
	const ordlist<T>& operator=(const ordlist<T>&);
	void print();

protected:
	int max_size;
	int legnth;
	T *list;
};
#endif

-----------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//ordlist.cpp
#include"ordlist.h"

template <class T>
ordlist<T>::ordlist(int size1)
{     
	if(size1<0)
           {cout<<"The size must be positive"<<endl;
max_size=100;}
else 
	max_size=size1;
	legnth=0;
list=new T[max_size];

	
}
template <class T>
	ordlist<T>::~ordlist()
	{
		delete [] list;
		
		
	}
	template <class T>
	void ordlist<T>::insert(const ordlist<T>& insertitem)
	{
		int location;
		if(legnth==0)
			list[legnth++]=insetitem;
		else if(legnth==max_size)
			cout<<"list is full"<<endl;
		else 
		{
			loc=search(insertitem);
			if(location=-1)
				list[legnth++] =insertitem;
			else 
				cout<<"is in list"<<endl;
		}


	}
	template <class T>
	void ordlist<T>::remove(const ordlist& removeitem)
	{int location;
		if(legnth==0)
			cout<<"can't delete from an empty array"<<endl;
		else 
			{location=search(removeitem);
		if(location!=-1)
		removeat(location);
		else 
			cout<<"The item is not found"<<endl;}

	}
	template <class T>
	void ordlist<T>::removeat(int location)
	{
		if(location<0 || location>=legnth)
			cout<<"cannot delete"<<endl;
		else
			{for(int i=location;i<legnth-1;i++)
				list[i]=list[i+1];
		legnth--;
		}
	}
	template <class T>
	int ordlist<T>::search(const ordlist& item) 
	{
		int location;
		bool found=false;
		for(location=0;location<legnth;location++)
			if(list[location]==item)
			{
				found=true;
				break;
			}
			if(found)
				return location;
			else 
				return -1;

				
	}
	template <class T>
	bool ordlist<T>::isEmpty()
	{
		return(legnth==0);
	}
	template <class T>
	bool ordlist<T>::isFull()
	{
		return(legnth==max_size);
	}
	template <class T>
	ordlist<T>::ordlist(const ordlist<T>&a)
	{
		max_size=a.max_size;
		legnth=a.legnth;
		list= new T[max_size];

		for(int i=0;i<legnth;i++)
			list[i]=a.list[i];
	}
	template <class T>
	const ordlist<T>& ordlist<T>::operator=(const ordlist<T>&a)
	{
		if(this!=&a)
			{
			delete []list;
		max_size=a.max_size;
		legnth=a.legnth;
		list=new T[max_size];
		for(int i=0;i<legnth;i++)
			list[i]=a.list[i];
		}
		return *this;
	}
	template <class T>
	void ordlist<T>::print()
	{
		for(int i=0;i<legnth;i++)
			cout<<list[i]<<"  "<<endl;
	}


-------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include"ordlist.h"
//main.cpp
#include<iostream>
using namespace std;


int main()
{
	ordlist<int>ob(4);
	ob.insert(1);
	ob.insert(1);
	ob.insert(1);
	ob.insert(1);

	ob.print();

	
	
	return 0;
}


and this is error
main.obj : error LNK2019: unresolved external symbol "public: void __thiscall ordlist<int>::print(void)" (?print@?$ordlist@H@@QAEXXZ) referenced in function _main
main.obj : error LNK2019: unresolved external symbol "public: __thiscall ordlist<int>::~ordlist<int>(void)" (??1?$ordlist@H@@QAE@XZ) referenced in function _main
main.obj : error LNK2019: unresolved external symbol "public: void __thiscall ordlist<int>::insert(class ordlist<int> const &)" (?insert@?$ordlist@H@@QAEXABV1@@Z) referenced in function _main
main.obj : error LNK2019: unresolved external symbol "public: __thiscall ordlist<int>::ordlist<int>(int)" (??0?$ordlist@H@@QAE@H@Z) referenced in function _main
c:\users\abo abbas\documents\visual studio 2010\Projects\hw5555\Debug\hw5555.exe : fatal error LNK1120: 4 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
-----------------------------
help me and thanks
This is something you probably can't know and I really didn't find a way to do it the way you want i to either:

The following:

When you use a templaste class: you have to define the methods inside the class body or below but in the .h file.
1
2
3
4
5
6
7
8
9
template <class T>
class ordlist
{
public:
	ordlist(int size1) { //impelemnt constructor here};
	~ordlist() {//implement destructor here};
	void print(){//implement this method and all others here as well};
// declarate and define all other methods here as well you can't do it outside in a cpp
};


edit: listen to this guy below he's smarter ;x ^.^
Last edited on
You have to define template class methods in the same compilation unit. So if you have an int main somewhere that uses this template class, it needs the definitions of templated functions in the same object file. Typically for a templated class I put the implementation in the same header file like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef FOO_H
#define FOO_H

template <class T>
class Foo
{
public:
    T func();
    Foo();
    virtual ~Foo();
};

template <class T>
T Foo<T>::func()
{
    T t;
    return t;
}

template <class T>
Foo<T>::Foo() {}

template <class T>
Foo<T>::~Foo() {}

#endif 


Then later on whatever code uses class Foo would just include Foo.h
Last edited on
I tried what you say to me but display same error :( :( :(
@Glandy: explicit template instantiation http://www.cplusplus.com/forum/articles/14272/
@OP: you may did it incorrectly, update your code
Thank you all now its working

but i have question
if i want to ascending list like 1 2 3 4 5

how can i do it in my code ?
Topic archived. No new replies allowed.