C++ Using a class template argument as a template argument for another type
- by toefel
Hey Everyone,
I'm having this problem while writing my own HashTable. It all works, but when I try to templatize the thing, it gave me errors. I recreated the problem as follows:
THIS CODE WORKS:
typedef double Item;
class A
{
public:
A()
{
v.push_back(pair<string, Item>("hey", 5.0));
}
void iterate()
{
for(Iterator iter = v.begin(); iter != v.end(); ++iter)
cout << iter->first << ", " << iter->second << endl;
}
private:
vector<pair<string, double> > v;
typedef vector< pair<string, double> >::iterator Iterator;
};
THIS CODE DOES NOT:
template<typename ValueType>
class B
{
public:
B(){}
void iterate()
{
for(Iterator iter = v.begin(); iter != v.end(); ++iter)
cout << iter->first << ", " << iter->second << endl;
}
private:
vector<pair<string, ValueType> > v;
typedef vector< pair<string, ValueType> >::iterator Iterator;
};
the error messages:
g++ -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ..\main.cpp
..\main.cpp:50: error: type std::vector<std::pair<std::string, ValueType>, std::allocator<std::pair<std::string, ValueType> > >' is not derived from typeB'
..\main.cpp:50: error: ISO C++ forbids declaration of `iterator' with no type
..\main.cpp:50: error: expected `;' before "Iterator"
..\main.cpp: In member function `void B::iterate()':
..\main.cpp:44: error: `Iterator' was not declared in this scope
..\main.cpp:44: error: expected `;' before "iter"
..\main.cpp:44: error: `iter' was not declared in this scope
Does anybody know why this is happening? Thanks!