Linking errors

Here are the errors:
InputTest.obj : error LNK2019: unresolved external symbol "int __cdecl copyarray<char>(char * const,int)" (??$copyarray@D@@YAHQADH@Z) referenced in function "void __cdecl getdat(char *)" (?getdat@@YAXPAD@Z)


Here is the code for the header and cpp file I was trying to use:

Header file:

1
2
3
4
5
template <class ArrayType>
int getarraysize(ArrayType a[]);

template <class ArrayType>
int copyarray(ArrayType a[], int ns);

CPP File:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "stdafx.h"

template <class ArrayType>
int getarraysize(ArrayType a[]) {
	int c = 0;
	while ((a[c] != NULL) && (a[c] != '\0')) {
		c++;
	}
	return c;
}

template <class ArrayType>
int copyarray(ArrayType a[], int ns) {
	int c = 0; // index counter
	char* ca = new char[ns]; // new array
	int s = getarraysize(a);
	while (c <= s) {
		ca[c] = a[c];
		c++;
	}
	delete[] a;
	a = ca;
	return ns;
}


Here is the main CPP file where I was trying to include the header file in:
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
// InputTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h" // The header file I mentioned just now is included in stdafx.h
using namespace std;


void getdat(char* a) {
	char d = NULL;
	int c = 0;
	int s = 20;
	cin.get(a, s);
	if (a[getarraysize(a)]) {
		while ((d != NULL) && (d != '\0')) {
			cin.get(d);
			s = copyarray(a, s + 10);
			cin.get(d);
			c++;
			a[c] = d;
		}
	}
}

int _tmain(int argc, _TCHAR* argv[])
{	
	cout << "Insert data: ";
	char* buf = new char[20];
	getdat(buf);
	int c = 0;
	while (buf[c] != NULL) {
		cout << buf[c];
		c++;
	}
	char w;
	cin >> w;
	return 0;
}


Can anyone tell me why these linking errors happen?
Last edited on
Yes. Because you are trying to use the template in a different compilation unit (A different .cpp from which it was defined) it will not link. If you only used the template in the templates .cpp then your implementation is fine. Since you want to use it in main, you will need to put the template implementation from your .cpp file in the templates header file.
I did. Look at the header file.
Also, the reason I defined ArrayType twice in both the header and CPP file is the fact that one of the functions that was not with the template would complain of ArrayType being non-existent.
No, dude. You declared it in the header file, and defined it in the cpp file. With templates, you must declare and define in the same file.
how can I do both? I mean, the code I used for "defining" it and "declaring" it was the same.
No, you have it like this, and it's not correct:

whatever.h:
1
2
3
4
5
template <class ArrayType>  // Declaration
int getarraysize(ArrayType a[]);

template <class ArrayType>
int copyarray(ArrayType a[], int ns);


whatever.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
#include "stdafx.h"

template <class ArrayType>  // Definition
int getarraysize(ArrayType a[]) {
	int c = 0;
	while ((a[c] != NULL) && (a[c] != '\0')) {
		c++;
	}
	return c;
}

template <class ArrayType>
int copyarray(ArrayType a[], int ns) {
	int c = 0; // index counter
	char* ca = new char[ns]; // new array
	int s = getarraysize(a);
	while (c <= s) {
		ca[c] = a[c];
		c++;
	}
	delete[] a;
	a = ca;
	return ns;
}


This is correct:

whatever.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
#include "stdafx.h"

template <class ArrayType>  // Declaration
int getarraysize(ArrayType a[]);

template <class ArrayType>
int copyarray(ArrayType a[], int ns);

template <class ArrayType>  // Definition
int getarraysize(ArrayType a[]) {
	int c = 0;
	while ((a[c] != NULL) && (a[c] != '\0')) {
		c++;
	}
	return c;
}

template <class ArrayType>
int copyarray(ArrayType a[], int ns) {
	int c = 0; // index counter
	char* ca = new char[ns]; // new array
	int s = getarraysize(a);
	while (c <= s) {
		ca[c] = a[c];
		c++;
	}
	delete[] a;
	a = ca;
	return ns;
}


There is no whatever.h
Last edited on
Topic archived. No new replies allowed.