Linked List\Template problem

Hello All:
I need some help with a school project. I have code that I used a long time ago and worked at one point, but does not seem to work now. Since the linked list implementation is the data structure that I'm expected to use, I'm trying to make this code work if possible. I am using a template class for a linked list implementation, on VS2005. The code below is what I would like to use, and I can't get to compile. I get lots of errors, but I have only included a few. Since the first few errors that come up are with the linkedList.cpp, I expect that is where the problem lies, but I am including linkedList.h in-case someone see something there. I have looked over this multiple times, for multiple days now, and can't seem to find the problem.

linkList.cpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
template <class T>
void listClass<T>::destoryList()
{
    while (!isEmpty())
		{
			removeFirst();
		}
}

template <class T>
listClass<T>::listClass(const listClass<T> &source)//O(n)
{
    head=tail=NULL;
    nodeClass<T> *ptr=source.head;
    while(ptr != NULL)
    {
        addItemToTail(ptr->data);//n times
        ptr=ptr->next;
    }
}
template <class T>
listClass<T>::~listClass()//O(n)
{
    free();//1*n
}
template <class T>
void listClass<T>::makeEmpty()//O(n)
{
    free();
    head=tail=NULL;
}
template <class T>
const listClass<T>& listClass<T>::
    operator=(const listClass<T> &right)//O(n)
{
    //avoid self copy
    if(this==&right)
        return right;

    //delete memory
    this->free();

    //deep copy
    nodeClass<T> *ptr=right.head;
    while(ptr != NULL)
    {
        addItemToTail(ptr->data);//n time
        ptr=ptr->next;
    }

    //return
    return right;
}
template <class T>
void listClass<T>::addItemToHead(const T& item)//O(1)
{
    nodeClass<T> *ptr = new nodeClass<T>;
    ptr->data=item;
    if(isEmpty())
        head=tail=ptr;
    else
    {
        ptr->next=head;
        head=ptr;
    }
}
template <class T>
void listClass<T>::addItemToTail(const T& item)//O(1)
{
    if(isEmpty())
        addItemToHead(item);
    else
    {
        nodeClass<T> *ptr=new nodeClass<T>;
        ptr->data=item;
        tail->next=ptr;
        tail=tail->next;
    }
}
template <class T>
void listClass<T>::removeFirst()//O(1)
{
    if(!isEmpty())
    {
        nodeClass<T> *ptr=head;
        head=head->next;
        if(head==NULL)
            tail=NULL;
        delete ptr;
    }
    else
    {
        cerr << "Empty List\n";
        exit(41);
    }
}
template <class T>
void listClass<T>::removeLast()//O(n)
{
    if(isEmpty())
    {
        cerr << "Empty List\n";
        exit(41);
    }
    else
    {
        if(head==tail)
        {
            removeFirst();
        }
        else
        {
            nodeClass<T> *ptr=head;
            while(ptr->next != tail)
                ptr=ptr->next;
            delete tail;
            tail=ptr;
            tail->next=NULL;
        }
    }
}
template <class T>
const T& listClass<T>::getFirst()const//O(1)
{
    if(isEmpty())
    {
        cerr << "Empty List\n";
        exit(41);
    }
    return head->data;
}
template <class T>
const T& listClass<T>::getLast()const//O(1)
{
    if(isEmpty())
    {
        cerr << "Empty List\n";
        exit(41);
    }
    return tail->data;
}


linkedList.h
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
#include "linkedList.cpp"

#pragma once

#include <iostream>
using namespace std;
//
template <class T>
class listClass;

template <class T>
class nodeClass
{
private:
    T data;
    nodeClass<T> *next;
public:
    friend listClass<T>;
    nodeClass(){next=NULL;};
};

template <class T>
class listClass
{
private:
    nodeClass<T> *head, *tail;
public:
    listClass(){head=tail=NULL;};//O(1)
    listClass(const listClass<T> &source);//O(n)
    ~listClass();//O(n)
	void destroyList();
    void makeEmpty();//O(n)
    const listClass<T>& operator=(
        const listClass<T> &right);//O(n)
    bool isEmpty()const{return head==NULL;};//O(1)
    bool isFull()const{return false;};//O(1)
    void addItemToHead(const T& item);//O(1)
    void addItemToTail(const T& item);//O(1)
    void removeFirst();//O(1)
    void removeLast();//O(n)
    const T& getFirst()const;//O(1)
    const T& getLast()const;//O(1)
};

Errors:
Error 1 error C2143: syntax error : missing ';' before '<'
Error 2 error C2182: 'listClass' : illegal use of type 'void'
Error 3 error C2988: unrecognizable template declaration/definition
Error 4 error C2059: syntax error : '<'
Error 5 error C2039: 'destoryList' : is not a member of '`global namespace''
Error 6 error C2143: syntax error : missing ';' before '{'
Error 7 error C2447: '{' : missing function header (old-style formal list?)
Error 8 error C2143: syntax error : missing ';' before '<'
Error 9 error C4430: missing type specifier - int assumed.
Error 10 error C2086: 'int listClass' : redefinition
Error 11 error C2988: unrecognizable template declaration/definition
Error 12 error C2059: syntax error : '<'
Error 13 error C2588: '::~listClass' : illegal global destructor

Like I said above, I have been looking at this code for a couple of days, so I'm hoping someone sees something that I have just been overlooking.

Thanks for any advice or suggestions!!
Last edited on
I think you have mixed up the #includes.

The cpp file should include the h file, but the h file should not include the cpp file.

Hope this helps.
Thanks, I re-wrote the code from the beginning, and found that I had renamed a function and did not change it through out the code. Now I'm getting LNK errors, which I think is because I have not fully finished my main program, and used all the functions yet.

I will see what happens when I'm completely done.
There are two steps to building a program:

1. Compilation. The actual translation of your code into machine code. The machine code for each .cpp file gets placed in its own file, named the same but with an .o or .obj extension.

2. Linking. This takes the .o files and joins them into an executable. If you tell the compiler to do it all at once you won't see the object files (.o files) --the compiler will put them in a temporary directory and delete them when done.

When people say "compile a program" that typically implies linking also. The link stage will fail if the linker cannot resolve all dependencies. For example, every program must declare a main() function. So if you try to make an executable from code without main(), the linker will complain.


Typical practice on large projects is to compile .cpp files individually and keep the .o files around. Then when changes are made to the program only the .cpp files that need to be recompiled are. This is where things like make and other build systems come in. Or, if you are using an IDE, where the IDE project dependencies are used.

Let me know if you want to learn more.

Hope this helps.
Yeah, that makes sense. I'm done working on the base data structure ie, linked list based stack. But I'm still getting linking errors. I have a very basic main() right now, since I'm just trying to get the data structure to work before I dive into the rest of the project.

Errors:
Error 1 error LNK2028: unresolved token (0A000331) "public: __thiscall singleListClass<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::singleListClass<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(class singleListClass<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > const &)" (??0?$singleListClass@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@$$FQAE@ABV0@@Z) referenced in function "public: static void __clrcall singleListClass<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::<MarshalCopy>(class singleListClass<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > *,class singleListClass<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > *)" (?<MarshalCopy>@?$singleListClass@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@$$FSMXPAV1@0@Z) main.obj

Error 2 error LNK2028: unresolved token (0A000332) "public: __thiscall singleListClass<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::~singleListClass<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(void)" (??1?$singleListClass@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@$$FQAE@XZ) referenced in function "public: static void __clrcall singleListClass<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::<MarshalDestroy>(class singleListClass<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > *)" (?<MarshalDestroy>@?$singleListClass@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@$$FSMXPAV1@@Z) main.obj

Error 3 error LNK2028: unresolved token (0A000333) "public: void __thiscall singleListClass<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::removeLast(void)" (?removeLast@?$singleListClass@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@$$FQAEXXZ) referenced in function "void __cdecl print<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(class singleListClass<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >)" (??$print@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@$$FYAXV?$singleListClass@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@@Z) main.obj

Error 4 error LNK2028: unresolved token (0A000334) "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const & __thiscall singleListClass<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::getLast(void)const " (?getLast@?$singleListClass@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@$$FQBEABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function "void __cdecl print<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(class singleListClass<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >)" (??$print@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@$$FYAXV?$singleListClass@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@@Z) main.obj


If you would like to see the code again, I can post that as well. I have been searching for this linking errors for several hours, and can't seem to find a answer that works with this program.

What I have tried:
-Setting references in my link options.
-added an #include <cstdlib>

Again, any help is GREATLY appreciated.

Thanks, C
It looks like you are mixing calling conventions. One file is a CLR project and another isn't. Make sure they are both CLR or not.

Did that get it?
I have been researching the above suggestion, and I'm still not able to compile fully. I'm getting the above LNK errors still. Some of the files that I'm trying to use are imported, so I'm going to try to re-write them in a new file, and see if that doesn't make a difference.

If anyone else has any other suggestions, I would appercate it. Thanks Again!!
Last edited on
Topic archived. No new replies allowed.