Problems with Templates and User defined classes

First, Let me introduce myself.

I've been lurking on this forum for some time now and have found a plentitude of awesome resources to help me as I work in C++. Thanks for keeping such a trove of information around.

I'm a CS student have worked in Java, C#, and C++ thus far. Still have a few years to go.
---

The Question

Up until this point, I have generally been able to find good answers to my questions through the resources at hand (i.e. internet, prof, textbook, etc.)

Unfortunately the lab is closed, I have no class today, and I cannot find an answer to my problem online. Thus, I decided to finally create an account and ask it here.

The welcome post was quite clear that brevity and precision are appreciated here and though I tend to be long winded I will attempt to honor that rule.

One more thing: I use Visual Studios 2010 Express.

The Assignment:

1. Read from file and populate a vector of user defined 'Student' objects.

2. Write each of the following sort algorithms and sort the vector with each:

Selection Sort
Insertion Sort
Merge Sort
Quick Sort
std::sort (from the standard library)
std::list.sort() (from the standard library)

4. Track how many times each sort algorithm compares Student Objects.

5. Write to file and end.

---

What I've Done:

-I wrote the I/O and Student classes in a previous project.

-Added functionality to Student class to track number of times the
overloaded operator '<' or '>' are used

-Wrote Selection and Insertion Sort algorithms

---

The Problem:

Rather than writing my sort algorithms explicitly for Student objects, I decided to use templates to make them universal. Having done this and seeing no errors in the code I attempted to compile.

The project failed to build and output read as follows for both sort templates:

"while compiling class template member function 'std::vector<_Ty> selectionSort<T>::sort(std::vector<_Ty>)' with _Ty=Student, T=Student

lab4\program.cpp(165) : see reference to class template instantiation 'selectionSort<T>' being compiled with T=Student"

---

I understand this to mean that the compiler doesn't know what to do when I pass an object of type Student to my sort templates.

What I Have Done:

- I added a vector<int> in my program and ran the sort algorithms on it.
No errors were encountered, leading me to believe it is a problem with my
Student class.

- I have looked in my textbook for information on declaring and calling
Template classes. As far as I can tell I am doing this 'by the book.'

- Used Google, C++.com, the MSDN webpage discussing templates, and a few other
websites to look for suggestions. Nothing directly applies (at least not that
I can tell).

---

Anyone have any ideas?

My Student class has overloaded >, <, == operators, and getters and setters for a Student info (name, address, ID, etc). Nothing out of ordinary.

I did not post my code to keep the post short...or, at least shorter.
while compiling class template member function 'std::vector<_Ty> selectionSort<T>::sort(std::vector<_Ty>)' with _Ty=Student, T=Student

lab4\program.cpp(165) : see reference to class template instantiation 'selectionSort<T>' being compiled with T=Student

¿And the error is... ?
¿Why do you have 2 template parameters (T, _Ty) ?

I would like to see the function declaration and the call.
I suppose error was a poor description of my problem. Compilation problem would have been a more apt description(?)

I don't believe that I have two template parameters, though if I do that is probably my problem.

I haven't written templates in some time and never in C++. Here is the template declaration.

#include <vector>
using namespace std;
#pragma once

template<class T>

class selectionSort
{
public:
selectionSort(void);

~selectionSort(void);

vector<T> sort(vector<T>);
};

template <class T> selectionSort<T>::selectionSort() {}

template <class T> selectionSort<T>::~selectionSort() {}

template <class T> vector<T> selectionSort<T>::sort(vector<T> extVector)
{
.
.
.
}
---

Function call is as follows:

selectionSort<type> mySelectionSort;

mySelectionSort.sort(vector);

where type defines the type of object being sorted and where vector is a vector of those objects.

---

Thanks for the response.
Was that the complete error message? It looks incomplete to me (it would appear that the start of the error message is missing). Is it a compiler error or a linker error?


On a more general note - The simplest way to write template code is to write it without templates first. Once you've got it working the way you want for one type, converting it to a template is quite easy.
(Also, you shouldn't need a class for your Selection Sort - you're just overcomplicating it and giving yourself extra hassle which you don't need. The C++ Standard Libraries do this with standalone functions, so you can too!)

If you do it this way, then it can be helpful to drop in a typedef as a stop-gap for your template parameter. e.g.
1
2
3
4
5
6
7
typedef std::vector<Student> Container; 

/* template< typename Container >  -- uncomment me later! */
void SelectionSort( Container& data )
{
     // To-do: Selection sort algorithm
}
it is best to avoid the using namespace std; directive in a header file since it will pull in the entire std namespace every time you include your header which will eventually result in a conflict of identifier names. Just use the fully qualified name such as std::vector< T >.

Also in your class member sort function:

vector<T> sort(vector<T>);

You are passing the vector by value and returning by value which is going to result in inefficient copying for large vectors. You could pass by reference and make the return type void. Even if you need to preserve the original vector's sort order you could copy the vector before calling the sort routine. This would only need one copy of the vector whereas as you have it now two copies will have to be made, once when the parameter is copied and once when the vector is returned.

As for your compiling error, a more complete error message is needed.
Topic archived. No new replies allowed.