ifstream syntax error in a class template constructor

These are my errors:
line, error, default order, which file the error is in
16 error C2061: syntax error : identifier 'ifstream' 1 heap.h
07 error C2061: syntax error : identifier 'ifstream' 2 heap.cpp
35 error C2061: syntax error : identifier 'ifstream' 3 chunkList.h
21 error C2061: syntax error : identifier 'ifstream' 4 chunkList.cpp
35 error C2061: syntax error : identifier 'ifstream' 5 chunkList.h
16 error C2061: syntax error : identifier 'ifstream' 6 heap.h

You can see that it's hitting two lines twice.

I tried similar coding on a normal function, and didn't get any errors, so it's only happening inside my class constructor (I have two classes, but they use the same heading):

1
2
3
4
5
template <class TYPE>
heap <TYPE> ::heap(ifstream& fin, int NumOfNums)
{
...
}


I already #included fstream and my classes's header file in the driver.cpp file.
I'm using Visual Studio 9.
Last edited on
Templates don't work that way.

The most common use of templates is generating functions that work on different types that have similar interfaces. For example,
1
2
3
4
5
6
7
8
9
10
11
12
13
template <typename T>
void append_hello(T &a){
    a.push_back('h');
    a.push_back('e');
    a.push_back('l');
    a.push_back('l');
    a.push_back('o');
}
//...
std::vector<char> a;
std::string<char> b;
append_hello(a);
append_hello(b);

The part behind the :: is used by the compiler to generate appropriate code and check types and scopes. For example, to know what type the this pointer is. If it was possible to remove that information, the compiler would have no way of knowing what member function that definition is defining. Is it A::heap() or B::heap()?

if both classes have the functions with the same interface, you'll have to write both signatures. If you're feeling really lazy, you could use macros, although this is only used when there's a large number of similar functions, if it's used at all:
1
2
3
4
5
6
7
8
9
#define IM_A_LAZY_BUM(type) heap type::heap(ifstream& fin, int NumOfNums)

IM_A_LAZY_BUM(A){
//...
}

IM_A_LAZY_BUM(B){
//...
}
But this doesn't really make sense if you don't expect the signatures to change.
Topic archived. No new replies allowed.