Function Template Already Defined

I've searched online and found several topics with the same error, but I still can't figure this out. I think the issue might be might with #includes.

I first thought I had to remove the #include dlinkedlist.h from the cpp file, but then that prevents syntax highlighting and causes syntax errors. However, if I do include it, the syntax highlight returns but the function template already define error comes up.

I thought I used guards properly but I guess not.

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
//////////////////////////////////////////////
//////////////dlinkedlist.cpp/////////////////
//////////////////////////////////////////////
#include <iostream>
#include "dlinkedlist.h"
 
//default constructor
template <class T>
DLinkedList<T>::DLinkedList(){
    size = 0;
    front = NULL;
    back = NULL;
}
 
template <class T>
void DLinkedList<T>::DeleteList() {
}
 
template <class T>
void DLinkedList<T>::CopyList(const DLinkedList& ll) {
}
 
template <class T>
void DLinkedList<T>::InsertFront(T item) {
}


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
///////////////////////////////////
//////dlinkedlist.h////////////////
///////////////////////////////////
#ifndef _DLINKEDLIST_H_
#define _DLINKEDLIST_H_
 
#include <cstdlib>
#include <stdexcept>
#include <string>
 
using namespace std;
 
// template class for doubly-linked list node
template <class T>
class Node
{
public:
    T data;
    //string data;
    Node<T>* prev;
    Node<T>* next;
 
    // default constructor
    template <class T>
    Node(T value)
    {
        data = value;
        prev = NULL;
        next = NULL;
    }
};
 
// DLinkedList class definition
template <class T>
class DLinkedList
{
private:
    // DLinkedList private members
    int size; // number of items stored in list
    Node<T>* front; // references to the front
    Node<T>* back;  //  and back of the list
 
                    // helper function for deep copy
                    // Used by copy constructor and operator=
    void CopyList(const DLinkedList& ll);
 
    // helper function for deep delete
    // Used by destructor and copy/assignment
    void DeleteList();
 
public:
    // default constructor
    DLinkedList();
 
    // copy constructor, performs deep copy of list elements
    DLinkedList(const DLinkedList& ll);
};
 
#include "dlinkedlist.cpp"
#endif 
put them all in the header and drop the cpp file. Thats what the C++ Standard Library does.

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
#include <cstdlib>
#include <stdexcept>
#include <string>
 
using namespace std;
 
// template class for doubly-linked list node
template <class T>
class Node
{
public:
    T data;
    //string data;
    Node<T>* prev;
    Node<T>* next;
 
    // default constructor
    Node(T value)
    {
        data = value;
        prev = NULL;
        next = NULL;
    }
};
 
// DLinkedList class definition
template <class T>
class DLinkedList
{
private:
    // DLinkedList private members
    int size; // number of items stored in list
    Node<T>* front; // references to the front
    Node<T>* back;  //  and back of the list
 
                    // helper function for deep copy
                    // Used by copy constructor and operator=
    void CopyList(const DLinkedList& ll);
 
    // helper function for deep delete
    // Used by destructor and copy/assignment
    void DeleteList();
 
public:
    // default constructor
    DLinkedList();
 
    // copy constructor, performs deep copy of list elements
    DLinkedList(const DLinkedList& ll);
    
    void InsertFront(T item);
};

#include "source.cpp" 


.cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//default constructor
template <class T>
DLinkedList<T>::DLinkedList(){
    size = 0;
    front = NULL;
    back = NULL;
}
 
template <class T>
void DLinkedList<T>::DeleteList() {
}
 
template <class T>
void DLinkedList<T>::CopyList(const DLinkedList& ll) {
}
 
template <class T>
void DLinkedList<T>::InsertFront(T item) {
}
Last edited on
Thanks for the suggestion Jaybobo. Your method was how I would've done it, but unfortunately my assignment requires the implementation to be in a cpp file.
i edited my post to split .h and .cpp

I dont like this approach, including cpp files looks bad.

you cant put a template in a cpp file and compile it. well you can but it will be useless.

when you want to use the linked list in another class you will need to include the source for the entire template so the compiler can compile it with the template parameter. it will need the entire template class so you will need to include the cpp also.

this is why template libraries are usually header only.
Topic archived. No new replies allowed.