How to call a template class?

Hi,

I'm writing my first template class for an assignment, and must admit I'm really having to feel this out as I go (it would help if we were given notes on how to do it...)

Anyway,I'm getting bizarre errors when trying to compile my code. I have a header/definition file and a driver file, and I'm not entirely sure how one should go about declaring a object from the template class. This is my big need right now, though I'm getting other bizarre errors, for some reason.

Here's the code. First, the header/definition 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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

// Class prototypes -------

template<class var>
class Sorter
{
public:    
    Sorter(
        string fileName
        );

    void sortValues(
        bool snapshot,
        bool timer,
        bool counter
        );

    void outputValues(
        char type,
        string fileName,
        int perLine,
        int width,
        int percision
        );

private:
    vector<var> values;
};


// Class Definition -------

template<class var>
class Sorter
{
    Sorter::Sorter(string fileName)
    {
        fstream inFile;
        infile.open(fileName);
        string nextValue;

        while(!infile.eof())
        {
            getline(inFile, nextValue, ' '); 
            values.push_back(nextValue);
            cout << nextValue << endl;  // <-- For debugging
        }
    }

    void Sorter::sortValues(){}

    void Sorter::outputValues(){}
}


And here's my driver file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

#include "SelectionSorter.hpp"

template<class var>
int main()
{
    Sorter<var> test("testFile.txt");
    cin.ignore(80, '\n');

}


And finally, here are the errors I'm getting, both related to the driver file:
testsortdriver.cpp(16): error C2143: syntax error : missing ';' before 'template'
testsortdriver.cpp(16): error C2953: 'Sorter' : class template has already been defined


So, any advice is appreciated. Also, if you can think of a decent resource for making template classes, that wouldn't be too shabby either. It'd be a huge help :)
Try removing line 9 in the driver file.
Post back re: remaining errors (if any).
Hi,

Thanks, for the suggestion, but I have tried removing line 9 (originally I didn't have it in). I do get other errors without this line, will have to post those shortly.

What's also weirding me out is that error saying it expects a semicolon before line 9 (the template declaration). I don't know why it wants one, but if I put one on the end of the #include line above it, I (rightly) get a preprocessing directive error. If I instead put the semicolon on the EMPTY line between the #include and the template declaration, it stops complaining! I have no idea what that's about.

EDIT: Just ran the code again, and the most consistent errors I get are that the class template has already been defined (re: the .hpp file), and that the class has no constructor (re: the .cpp driver file).
Last edited on
you forgot a semicolon after the class definition in the header file :)
lol, ty. :)

Will reply to this again, when I have another chance to run this.

Topic archived. No new replies allowed.