A strange link error

I defined a template class FieldSet and a struct TableNode.
Then I tried to claim "FieldSet<TableNode> fieldSet[TableMaxNumber]" in another class called DBOperations.
And then, a link error occured!
Codes and error messages are as following.
Can anyone tell me about my mistake and the way to fix it?
THANKS A LOT!!!
=========================================================================
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
// FieldSet.h: interface for the FieldSet class.
#define MaxSizeDefine 50
#define defaultSize 100
template<class Type> class FieldSet  
{
public:
    Type *data;
    FieldSet(int maxSize = defaultSize);
    virtual ~FieldSet();
    int find(Type &x) const;
    int add(Type &x);
    int add(Type &x,int y);
    int insert(Type &x,int i,int y);
    int remove(int i);  
    int remove(Type &x); 
    int getData(Type* x);
    int clearData();
private:
    int maxSize;
    int last;
};

//implementation of the FieldSet class.

template<class Type> FieldSet<Type>::FieldSet(int sz)
{
    if(sz>0){
        maxSize = sz;
        last=-1;
        data= new Type[maxSize];
    }
}

template<class Type> FieldSet<Type>::~FieldSet()
{    
}
......

===========================================================================
1
2
3
4
5
6
7
8
9
// TableNode1.h 
struct TableNode { 
   char sType[8]; 
   int iSize; 
   int tableChoice; 
   char bKey; 
   char bNullFlag; 
   char bValidFlag; 
};

===========================================================================
1
2
3
4
5
6
7
// DBOperations.h
#include "TableNode1.h"
#include "FieldSet.h"
.....
private:
    TableNode tempTN;
    FieldSet<TableNode> fieldSet[TableMaxNumber];

===========================================================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// DBOperations.cpp: implementation of the DBOperations class.
DBOperations::DBOperations()
{
    tnum=0;
    inum=0;
    tableChoice=0;
    bExist=false;
    while(tnum<TableMaxNumber){fnum[tnum]=0; tnum++;}
    tnum=0;
}

DBOperations::~DBOperations()
{    

}
.......

=======================================================================
1
2
3
4
5
6
7
8
9
10
11
12
13
ERROR MESSAGES:
Linking...
DBOperations.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall FieldSet <struct TableNode>::~FieldSet <struct TableNode>(void)" (??1?$FieldSet@UTableNode@@@@UAE@XZ)
DBOperations.obj : error LNK2001: unresolved external symbol "public: __thiscall FieldSet <struct TableNode>::FieldSet <struct TableNode>(int)" (??0?$FieldSet@UTableNode@@@@QAE@H@Z)
DBOperations.obj : error LNK2001: unresolved external symbol "public: int __thiscall FieldSet <struct TableNode>::add(struct TableNode &)" (?add@?$FieldSet@UTableNode@@@@QAEHAAUTableNode@@@Z)
DBOperations.obj : error LNK2001: unresolved external symbol "public: int __thiscall FieldSet <struct TableNode>::clearData(void)" (?clearData@?$FieldSet@UTableNode@@@@QAEHXZ)
DBOperations.obj : error LNK2001: unresolved external symbol "public: int __thiscall FieldSet <struct TableNode>::add(struct TableNode &,int)" (?add@?$FieldSet@UTableNode@@@@QAEHAAUTableNode@@H@Z)
DBOperations.obj : error LNK2001: unresolved external symbol "public: int __thiscall FieldSet <struct TableNode>::insert(struct TableNode &,int,int)" (?insert@?$FieldSet@UTableNode@@@@QAEHAAUTableNode@@HH@Z)
DBOperations.obj : error LNK2001: unresolved external symbol "public: int __thiscall FieldSet <struct TableNode>::remove(int)" (?remove@?$FieldSet@UTableNode@@@@QAEHH@Z)
Debug/SimpleDBMS_CPP.exe : fatal error LNK1120: 7 unresolved externals
Error executing link.exe.
Creating browse info file...

SimpleDBMS_CPP.exe - 8 error(s), 0 warning(s) 

Last edited on
Template based classes must defined in a single header file. So move the implementation for FieldSet into FieldSet.h

FieldSet.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
#ifndef _FIELD_SET_H_INCLUDED_
#define _FIELD_SET_H_INCLUDED_

#define defaultSize 100

template<class Type> class FieldSet
	{
	public:
		Type *data;
		FieldSet(int maxSize = defaultSize);
		virtual ~FieldSet();
		int find(Type &x) const;
		int add(Type &x);
		int add(Type &x,int y);
		int insert(Type &x,int i,int y);
		int remove(int i);
		int remove(Type &x);
		int getData(Type* x);
		int clearData();
	private:
		int maxSize;
		int last;
	};

template<class Type> FieldSet<Type>::FieldSet(int sz)
{
	if(sz>0){
		maxSize = sz;
		last=-1;
		data= new Type[maxSize];
	}
}

template<class Type> FieldSet<Type>::~FieldSet()
{
}

...
#endif 
Last edited on
thank you !
but the same error message occurred AFTER I moved the implementation to FieldSet.h
why would that happen?
Where did you implement functions of class FieldSet ?
And please use code tags.
in FieldSet.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
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
#define defaultSize 100          
#define MaxSizeDefine 50

template<class Type> class FieldSet  
{
public:
	Type *data;
	FieldSet(int maxSize = defaultSize);
	virtual ~FieldSet();
	int find(Type &x) const;   
	int add(Type &x);         
	int add(Type &x,int y);  
	int insert(Type &x,int i,int y); 
	int remove(int i);         
	int remove(Type &x);       
	int getData(Type* x);
	int clearData();
private:
//	Type *data;
	int maxSize;
	int last;
};

template<class Type> FieldSet<Type>::FieldSet(int sz)
{
	if(sz>0){
		maxSize = sz;
		last=-1;
		data= new Type[maxSize];
	}
}

template<class Type> FieldSet<Type>::~FieldSet()
{	
}

template <class Type> FieldSet<Type>::find(Type &x) const{
	int i = 0;
	while(i <= last && data[i] != x ) i++;
	if(i>last) return -1;
	else return i;
}

template <class Type> FieldSet<Type>::add(Type &x){
	if(last==maxSize-1){ 
		cout<<"Error: Max Size reached!";
		return 0;
	}
	else{
	last++;
	data[last]=x;
		return 1;
	}
}

template <class Type> FieldSet<Type>::add(Type &x,int y){  
	last=y-1;
	if(last==maxSize-1){ 
		cout<<"Error: Max Size reached!";
		return 0;
	}
	else{
	last++;
	data[last]=x;
		return 1;
	}
}

template <class Type> FieldSet<Type>::insert(Type &x,int i,int y){
	last=y-1;
	if(i<0||i>last+1||last==maxSize-1){
		cout<<"Insert Failed: Impossible Position."<<endl;
		return 0;
	}
	else{
		last++;
		for(int j=last;j>i;j--) data[j]=data[j-1];
		data[i] = x;
		cout<<"Instert Success!"<<endl;
		return 1;
	}
}

template <class Type> FieldSet<Type>::remove(int i){
	if (i>=0){
		last--;
		for(int j=i;j<=last;j++) data[j]=data[j+1];
		cout<<"Remove Success!"<<endl;
		return 1;
	}
	else {
		cout<<"Remove Failed: Impossible Position."<<endl;
		return 0;
	}
}

template <class Type> FieldSet<Type>::remove(Type &x){
	int i = find(x);
	remove(i);
}

template <class Type> FieldSet<Type>::getData(Type* x){ 
	x = data;
	return 0;
}

template <class Type> FieldSet<Type>::clearData(){
		maxSize = MaxSizeDefine;
		last=-1;
		data= new Type[maxSize];
		return 0;
}
Thank you, everyone!
I've fixed it with your help!
(by deleting FieldSet.cpp,and rebuild all)
Topic archived. No new replies allowed.