Error: LNK2019

First off, my code in its entirety is here: http://codepad.org/fcOLWhWs

I have three classes: an ordered list class which is composed of a binary search tree for implementation, the binary search tree is composed of a class called TreeNode in turn.

I know the main program for testing the implementation isn't done - but I got this error while trying to test the insert method. At the bottom of the paste, it shows my errors I'm receiving.

If I declare an instance of OLType, and I use -> to refer to something (which I know I can't because the instance isn't a pointer so no dereferencing needed) I get an error.

If I use a . (say myList.insert() ), it yells at me and tells me there has to be a class/struct/union on the left of the . operator - which there is.

If I declare myList to be a pointer to an object of OLType, I get yelled at because there are unresolved external symbols. However, in a fit of blind frustration, and trying to make my compiler as irritated as I was, I took the pointer and attempted the . operator on it, and the compiler said "Did you intend to use ->?" So...compiler/linker 2 - me 0. Can anyone tell me where I'm going wrong here?
Last edited on
Here's the code in case y'all prefer seeing it here as opposed to a pastebin:


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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//-----------------------TreeNode.h


#ifndef TREENODE__H
#define TREENODE__H

template <class T>
struct TreeNode {
  T item;
  TreeNode<T> *left;
  TreeNode<T> *right;
};

#endif

//-------------------BST.h

#ifndef BST__H
#define BST__H

#include "TreeNode.h"
#include <iostream>
  using namespace std;
enum TravType {PRE, POST, IN};



template <class T>
class BST {
  protected:
    TreeNode<T> *root;
    size_t count;
  public:
    BST();
    ~BST();
    const bool empty();
    void erase(const T&);
    const bool find(const T&);
    void insert(const T&);
    const size_t size();
    const void traverse(ostream&, TravType);
  private:
    T predecessor(TreeNode<T>*);
    void destroy(TreeNode<T>*&);
    const bool find_R(const T&, TreeNode<T>*);
    void erase_R(const T&, TreeNode<T>*&);
    void insert_R(const T&, TreeNode<T>*&);
    const void inorder(ostream&, TreeNode<T>*);
    const void postorder(ostream&, TreeNode<T>*);
    const void preorder(ostream&, TreeNode<T>*);

};

template <class T>
BST<T>::~BST() {
  destroy(root);
}
template <class T>
const bool BST<T>::empty() {
  return count == 0;
}
template <class T>
void BST<T>::erase(const T& item) {
  erase_R(item, root);
}
template <class T>
const bool BST<T>::find(const T& item) {
  return find_R(item, root);
}

template <class T>
void BST<T>::insert(const T& item) {
  insert_R(item, root);
}

template <class T>
const size_t BST<T>::size() {
  return count;
}

template <class T>
const void BST<T>::traverse(ostream& out, TravType trav) {
  if(trav == PRE)
    preorder(out, root);
  else if(trav == POST)
    postorder(out, root);
  else
    inorder(out, root);
}

template <class T>
void BST<T>::destroy(TreeNode<T> *&root) {
  root = NULL;
  count = 0;
}


template <class T>
const bool BST<T>::find_R(const T& item, TreeNode<T> *root) {
  if(item < root->item)
    find_R(item, root->left);
  else if(item > root->item)
    find_R(item, root->right);
  return item == root->item;
}

template <class T>
void BST<T>::erase_R(const T& item, TreeNode<T> *&root) {


  if(item < root->item) {
    predecessor = root;
    erase_R(item, root->left);
  }
  else if(item > root->item) {
    predecessor = root;
    erase_R(item, root->right);
  }

  else if(item == root->item) {
    //-----Case 1: No children
    if((root->left == NULL) && (root->right == NULL)) {
      if(predecessor->left == root)
        predecessor->left = NULL;
      else
        predecessor->right = NULL;
      delete root;
      count--;
    }

    else if((root->left != NULL) && (root->right == NULL)) {
      if(predecessor->left == root) 
        predecessor->left = root->left;
      else
        predecessor->right = root->left;
      delete root;
      count--;
    }

    else if((root->left == NULL) && (root->right != NULL)) {
      if(predecessor->left == root)
        predecessor->left = root->right;
      else
        predecessor->right = root->right;
      delete root;
      count--;
    }

    else if((root->left != NULL) && (root->right != NULL)) {
      TreeNode<T> *temp;  
      temp = root->left;
      while(temp->right != NULL) {
        predecessor = temp;
        temp = temp->right;
      }
      predecessor->right = temp->left;
      delete temp;
      count--;
    }
  }

}
template <class T>
const void BST<T>::inorder(ostream& out, TreeNode<T> *root) {
  if(root->left != NULL)
    inorder(out, root->left);
  out << " " << root->item;
}
template <class T>
const void BST<T>::postorder(ostream& out, TreeNode<T> *root) {
  if(root->left != NULL)
    postorder(out, root->left);
  if(root->right != NULL)
    postorder(out, root->right);
  out << " " << root->item;
}
template <class T>
const void BST<T>::preorder(ostream& out, TreeNode<T> *root) {
  out << " " << root->item;
  if(root->left != NULL)
    preorder(out, root->left);
  if(root->right != NULL)
    preorder(out, root->right);
}
#endif

//---------------------OLType.h

#ifndef OLTYPE__H
#define OLTYPE__H

#include "BST.h"


template <class T>
class OLType {
  friend ostream& operator << (ostream& out, const OLType<T>& myList) {
    if(trav == PRE)
      myList.traverse(out, PRE);
    else if(trav == POST)
      myList.traverse(out, POST);
    else
      myList.traverse(out, IN);
  }
  public:
    OLType();
    const bool empty();
    const bool find(const T&);
    void erase(const T&);
    void insert(const T&);
    const size_t size();
  protected: 
    BST<T> list;
};
template <class T>
const bool OLType<T>::empty() {
  return list.empty();
}

template <class T>
const bool OLType<T>::find(const T& item) {
  return list.find(item);
}

template <class T>
void OLType<T>::erase(const T& item) {
  list.erase(item);
}

template <class T>
void OLType<T>::insert(const T& item) {
  list.insert(item);
}

template <class T>
const size_t OLType<T>::size() {
  return list.size();
}

#endif


//------------------BSTmain.cpp

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

int main() {
  
     /* ERROR CASE 1:  
   OLType<int> myList;  
     ERROR CAUSED BY PRECEDING LINE: 
     1>  LINK : C:\Users\Nick\Documents\Visual Studio 2010\Projects\Binary\Debug\Binary.exe not found or not built by the last incremental link; performing full link

     1>BSTmain.obj : error LNK2019: unresolved external symbol "public: __thiscall OLType<int>::OLType<int>(void)" (??0?$OLType@H@@QAE@XZ) referenced in function _main

     1>C:\Users\Nick\Documents\Visual Studio 2010\Projects\Binary\Debug\Binary.exe : fatal error LNK1120: 1 unresolved externals */

     /* ERROR CASE 2:
   OLType<int> *myList; // No error by this statement alone...however...
   myList = new OLType<int>();
     ERROR CAUSED BY PRECEDING LINE:
     1>BSTmain.obj : error LNK2019: unresolved external symbol "public: __thiscall OLType<int>::OLType<int>(void)" (??0?$OLType@H@@QAE@XZ) referenced in function _main
     1>C:\Users\Nick\Documents\Visual Studio 2010\Projects\Binary\Debug\Binary.exe : fatal error LNK1120: 1 unresolved externals
     */
  int choice = 0;
  int num = 0;
}
You don't seem to ever define your constructor for your OLType class. Also, the constructor should probably be a template.
Okay I figured out the problem. In the class declarations, I included prototypes or declarations or whatever you wanna call 'em for the default constructors. However, because they're "default", I didn't think I'd have to put in a declaration. So...if you're reading this trying to find a solution to the same problem:

In your class method definitions, you might need to define the default constructors:

1
2
3
class::class() {

}


Orrrr if it's a template (just in case):

1
2
3
4
template <class T>
class<T>::class() {

}
That's exactly what it was firedraco. I didn't have any sort of constructor defined. Sorry for the delay in responding - I was fixing it when you posted, then I went and kicked myself hehe.
Topic archived. No new replies allowed.