Template Function issue

I'm working on an assignment and I'm learning how to use templates. Based on the instructions given, I'm not sure where I've gone wrong.

I'm getting an error message when trying to compile in my function getElementAt. The instructor has supplied the code for the main.cpp. I'm not sure where I'm going wrong here. Just to give some insight, the instructions are:

getElementAt
o Returns an element at a given index
 The index is passed as an integer
 The return value is a constant and it is returned as a reference

Any assistance is appreciated. Thank you.

Here is the main section where there is some trouble
1
2
3
4
  int usedArray = objectArray.getNumberUsed();
	cout << "\nArray of objects:\n";
	for (int i = 0; i < usedArray; ++i)
		(objectArray.getElementAt(i)).print();


Here is my code for this function:
1
2
3
4
5
template<typename T>
int DArray<T>::getElementAt(int idx) const
{
	return a[idx];
}


And this is the error I get from the compiler:
1>e:\cs 250\wk 6\ex_10_darray_template\project\main.cpp(164): error C2228: left of '.print' must have class/struct/union
1> type is 'int'
> getElementAt
> Returns an element at a given index
> The index is passed as an integer
> The return value is a constant and it is returned as a reference

1
2
3
4
5
6
7
template<typename T>
//int DArray<T>::getElementAt(int idx) const
const T& DArray<T>::getElementAt(int idx) const
{
        // validate idx?
	return a[idx];
}
I tried that earlier and I get a new list of errors and just tried that again to make sure. This is what the compiler says:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1>  Generating Code...
1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall DArray<int>::DArray<int>(int)" (??0?$DArray@H@@QAE@H@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: void __thiscall DArray<int>::addElement(int const &)" (?addElement@?$DArray@H@@QAEXABH@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall DArray<int>::~DArray<int>(void)" (??1?$DArray@H@@QAE@XZ) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: int __thiscall DArray<int>::getNumberUsed(void)const " (?getNumberUsed@?$DArray@H@@QBEHXZ) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: int const & __thiscall DArray<int>::getElementAt(int)const " (?getElementAt@?$DArray@H@@QBEABHH@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall DArray<double>::DArray<double>(int)" (??0?$DArray@N@@QAE@H@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: void __thiscall DArray<double>::addElement(double const &)" (?addElement@?$DArray@N@@QAEXABN@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall DArray<double>::~DArray<double>(void)" (??1?$DArray@N@@QAE@XZ) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: int __thiscall DArray<double>::getNumberUsed(void)const " (?getNumberUsed@?$DArray@N@@QBEHXZ) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: double const & __thiscall DArray<double>::getElementAt(int)const " (?getElementAt@?$DArray@N@@QBEABNH@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall DArray<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::DArray<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(int)" (??0?$DArray@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE@H@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: void __thiscall DArray<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::addElement(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?addElement@?$DArray@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall DArray<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::~DArray<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(void)" (??1?$DArray@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE@XZ) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: int __thiscall DArray<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::getNumberUsed(void)const " (?getNumberUsed@?$DArray@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QBEHXZ) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const & __thiscall DArray<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::getElementAt(int)const " (?getElementAt@?$DArray@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QBEABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall DArray<class Student>::DArray<class Student>(int)" (??0?$DArray@VStudent@@@@QAE@H@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: void __thiscall DArray<class Student>::addElement(class Student const &)" (?addElement@?$DArray@VStudent@@@@QAEXABVStudent@@@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall DArray<class Student>::~DArray<class Student>(void)" (??1?$DArray@VStudent@@@@QAE@XZ) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: int __thiscall DArray<class Student>::getNumberUsed(void)const " (?getNumberUsed@?$DArray@VStudent@@@@QBEHXZ) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: class Student const & __thiscall DArray<class Student>::getElementAt(int)const " (?getElementAt@?$DArray@VStudent@@@@QBEABVStudent@@H@Z) referenced in function _main
1>E:\CS 250\Wk 6\ex_10_darray_template\Debug\Project.exe : fatal error LNK1120: 20 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I have no idea what that means??
Put the definitions of the member functions of the template class in the header file.
Thank you for your help. I'm still having some trouble but don't want to be a bother.
Say what you're having trouble with, the reason everyone is here is to help or get help.
I don't think there is a template class. This is what was provided in the main 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
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
//This program creates partially-filled arrays
//  of integers.

#include "DArray.h"
#include "Student.h"

#include <iostream>
#include <string>

using namespace std;

int main( )
{
	////Ask the user to enter the capacity
	//cout << "Enter the capacity of the array: ";
	//int cap;
	//cin >> cap;

	////Create the new array
	//DArray a1(cap);

	////Ask the user to enter values for the array
	//cout << "\nEnter positive numbers "
	//	<< "(enter -1 to quit)" << endl;
	//int n;
	//cin >> n;
	//while(n != -1)
	//{
	//	a1.addElement(n);
	//	cin >> n;
	//}

	//Ask the user to enter the capacity			
	cout << "Enter the capacity of the array: ";
	int intCap;
	cin >> intCap;

	DArray<int> intArray(intCap);

	//Ask the user to enter values for the array
	cout << "\nEnter positive numbers "
		<< "(enter -1 to quit):" << endl;
	int n;
	cin >> n;
	while (n != -1)
	{
		intArray.addElement(n);
		cin >> n;
	}

	//Print array
	int usedInt = intArray.getNumberUsed();
	cout << "Array of integers: ";
	for (int i = 0; i < usedInt; ++i)
		cout << intArray.getElementAt(i) << " ";
	cout << endl;

	/********************************************
	* Create an array of doubles by asking		*
	*   the user to enter doubles.				*
	********************************************/

	//Ask the user to enter the capacity			
	cout << "\nEnter the capacity of the array: ";
	int doubleCap;
	cin >> doubleCap;

	DArray<double> doubleArray(doubleCap);

	//Ask the user to enter values for the array
	cout << "\nEnter positive decimal numbers "
		<< "(enter -1 to quit):" << endl;
	double d;
	cin >> d;
	while (d != -1)
	{
		doubleArray.addElement(d);
		cin >> d;
	}

	//Print array
	int usedDoubles = doubleArray.getNumberUsed();
	cout << "Array of doubles: ";
	for (int i = 0; i < usedDoubles; ++i)
		cout << doubleArray.getElementAt(i) << " ";
	cout << endl;

	/********************************************
	* Create an array of strings by asking		*
	*   the user to enter strings.				*
	********************************************/

	//Ask the user to enter the capacity			
	cout << "\nEnter the capacity of the array: ";
	int stringCap;
	cin >> stringCap;

	DArray<string> stringArray(stringCap);

	//Ask the user to enter values for the array
	cout << "\nEnter words "
		<< "(enter -1 to quit):" << endl;
	string s;
	cin >> s;
	while (s != "-1")
	{
		stringArray.addElement(s);
		cin >> s;
	}

	//Print array
	int usedStrings = stringArray.getNumberUsed();
	cout << "Array of strings: ";
	for (int i = 0; i < usedStrings; ++i)
		cout << stringArray.getElementAt(i) << " ";
	cout << endl;

	/********************************************
	* Create an array of objects of type		*
	*	Student. Ask the user to enter data.	*
	* Print the array using the overloaded		*
	*   operator[]								*
	********************************************/

	//Ask the user to enter the capacity			
	cout << "\nEnter the capacity of the array: ";
	int objectCap;
	cin >> objectCap;

	DArray<Student> objectArray(objectCap);
	//Ask the user to enter values for the array
	int i = 0;
	char answer = 'y';
	int id = 0;
	string major;

	while (answer == 'y')
	{
		cout << "\nEnter student id and major " << endl;
		cin >> id >> major;

		Student st(id, major);

		objectArray.addElement(st);

		++i;

		if (i < objectCap)
		{
			cout << "\nAnother student (y/n)? ";
			cin >> answer;
		}
		else
		{
			answer = 'n';
		}

	}

	//Print array
	int usedArray = objectArray.getNumberUsed();
	cout << "\nArray of objects:\n";
	for (int i = 0; i < usedArray; ++i)
		(objectArray.getElementAt(i)).print();

	cout << endl;
	system("Pause");
    return 0;
}


This is what my header file says, I'm not sure what to put there:

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
#ifndef DARRAY_H
#define DARRAY_H

#include <iostream>
using namespace std;

template<typename T>

class DArray
{
public:
	DArray();
    //Default constructor -	initializes array 
	//						with a capacity of 50.

	DArray(int);
	//Constructor - initializes array with a given capacity.
	//@param int - capacity of the array.

	void addElement(const T&);
    //addElement - adds an element to the array.
	//@param int - the element to add to the array.

	~DArray();
	//Destructor

	bool  full() const;
	int  getCapacity() const;
	int  getNumberUsed() const;
	void emptyArray();
	const T& getElementAt(int idx) const;

private:
    int *a;			//will point to an int (it will be an array)
    int capacity;	//capacity of the array
    int used;		//total number of elements in the array
};
#endif 


I feel dumb, not knowing, I'm sure it is something simple, I just haven't learned it yet. :(
> I don't think there is a template class.

template< typename T > class DArray is a template.


Put the definitions of the member functions of the template class in the header 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
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
/////////////// header DArray.h ///////////////////
#ifndef DARRAY_H
#define DARRAY_H

#include <iostream>
using namespace std;

template<typename T>

class DArray
{
public:
	DArray();
    //Default constructor -	initializes array 
	//						with a capacity of 50.

	DArray(int);
	//Constructor - initializes array with a given capacity.
	//@param int - capacity of the array.

	void addElement(const T&);
    //addElement - adds an element to the array.
	//@param int - the element to add to the array.

	~DArray();
	//Destructor

	bool  full() const;
	int  getCapacity() const;
	int  getNumberUsed() const;
	void emptyArray();
	const T& getElementAt(int idx) const;

private:
    int *a;			//will point to an int (it will be an array)
    int capacity;	//capacity of the array
    int used;		//total number of elements in the array
};

//////////////// definition of  getElementAt in the header //////////////////
template<typename T>
const T& DArray<T>::getElementAt(int idx) const
{
        // validate idx?
	return a[idx];
}

// put the definition of other member functions here
// for example, 
template<typename T>
void DArray<T>::emptyArray()
{
     // TO DO: ...
}

// etc.

#endif  
I think I'm going to give up on this for the night. I did what you said and that brought a whole new list of errors. I do appreciate your help though. Thank you.
Topic archived. No new replies allowed.