error: undefined reference to

Hi There
When I compile , I am getting the following error ie. "error: ld returned 1 exit status"

Could you please point me, what I am doing wrong ?


Errror:
=======
/../src/myCPP_linklist_02.cpp:16: undefined reference to `LinkedList<double>::~LinkedList()'
collect2.exe: error: ld returned 1 exit status


1
2
3
4
5
6
7
8
9
10
11
 
//  myCPP_linklist_02.cpp

#include <iostream>
#include "myCPP_linklist_02.h"
using namespace std;

int main() {
	LinkedList<double>  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
29
30
31
32
33
34
35
36
37
38
39
40
41
42

// myCPP_linklist_02.h

#include <iostream>
using namespace std;

#ifndef MYCPP_LINKLIST_02_H_
#define MYCPP_LINKLIST_02_H_


template <class T>
class LinkedList {

	struct ListNode {
		T value;
		struct ListNode *next;
	};


	int count = 0;
	ListNode *head ;

public:
	LinkedList(){
		head = nullptr;
	}
	~LinkedList();
	void appendNode(T);
	void insertNode(T);
	void insertNodeM(T num);
	void deleteNode(T);
	void displayList() ;


};




#endif /* MYCPP_LINKLIST_02_H_ */



Last edited on
closed account (SECMoG1T)
where is your class definition we need to see it too.
Please find class definition in my 2nd code ie. " myCPP_linklist_02.h" , It is template. I provided all the code I have.


template <class T>
class LinkedList {
Last edited on
closed account (SECMoG1T)
you have not yet defined any functions from your template class, you need to do that before you can instantiate an object of your class type in main().

that is why you are getting the errors
Last edited on
Got it. Thanks.
Here is the simple working code,


1
2
3
4
5
6
7
8
9
10
11
12
13

#include <iostream>

#include "myCPP_linklist_02.h"
using namespace std;

int main() {

	LinkedList<double>  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
29
30
31
32
33
34
35
36

/*
 * myCPP_linklist_02.h
 *
 */

#include <iostream>
using namespace std;

#ifndef MYCPP_LINKLIST_02_H_
#define MYCPP_LINKLIST_02_H_


template <class T>
class LinkedList {

	struct ListNode {
		T value;
		struct ListNode *next;
	};


	int count = 0;
	ListNode *head ;

public:
	LinkedList(){
		head = nullptr;
	}

};




#endif /* MYCPP_LINKLIST_02_H_ */ 

closed account (SECMoG1T)
we all start from there, so next step you try whatever you can to define the following functions.

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
///#include <iostream>
///using namespace std;

#ifndef MYCPP_LINKLIST_02_H_
#define MYCPP_LINKLIST_02_H_


template <typename T>
class LinkedList {

	struct ListNode {
		T value;
		struct ListNode *next;
	};

	int count ;
	ListNode *head ;

public:
	LinkedList();
	~LinkedList();
	void appendNode(T);
	void insertNode(T);
	void insertNodeM(T num);
	void deleteNode(T);
	void displayList() ;


};

template<typename T>
LinkedList<T>::LinkedList()
:count(0),head(nullptr){} ///you have defined this

///define this functions
template<typename T>
LinkedList<T>::~LinkedList()
{
    ///what code runs here
}

template<typename T>
LinkedList<T>::appendNode(T value)
{
    //what code runs here
}

template<typename T>
LinkedList<T>::insertNode(T value)
{
    ///what code runs here
}

template<typename T>
LinkedList<T>::insertNodeM(T num)
{
    ///what code runs here
}

template<typename T>
LinkedList<T>::deleteNode(T value)
{
    ///what code runs here
}

template<typename T>
LinkedList<T>::displayList()
{
    ///what code runs here
}



#endif /* MYCPP_LINKLIST_02_H_ */
yes. THANKS.
Topic archived. No new replies allowed.