Reputation: 379
I have a simple c++ program , which I am not able to compile , although i tried to search in google and try to read about template , inheritance and vector , but i didn't got any clue that what mistake I am doing, Can anyone please help me!! following is the code:
template <class T>
class Base
{
public:
int entries;
};
template <class T>
class Derive : public Base<T *>
{
public:
int j;
void pankaj(){j = entries;}
void clear();
};
template <class T> void Derive<T>::clear()
{
int i;
int j=entries;
};
int main()
{
Derive b1;
}
And I am getting following error: pankajkk> g++ sample.cpp
sample.cpp: In member function 'void Derive<T>::pankaj()':
sample.cpp:14: error: 'entries' was not declared in this scope
sample.cpp: In member function 'void Derive<T>::clear()':
sample.cpp:22: error: 'entries' was not declared in this scope
sample.cpp: In function 'int main()':
sample.cpp:26: error: missing template arguments before 'b1'
sample.cpp:26: error: expected `;' before 'b1'
Thanks!!
Upvotes: 2
Views: 192
Reputation: 27038
You must use this->foo
to access member variable foo
in template base classes. You may ask why.
Also, as Old Fox explains, you must specify the type T
when you declare your variable b1
.
template <class T>
class Base
{
public:
int entries;
};
template <class T>
class Derive : public Base<T *>
{
public:
int j;
void pankaj(){j = this->entries;}
void clear();
};
template <class T> void Derive<T>::clear()
{
int i;
int j=this->entries;
};
int main()
{
Derive<int> b1;
}
Upvotes: 2
Reputation: 8725
your main method has syntax error, change to:
int main()
{
Derive<int> b1;
}
you can put other type rather than integer...
in c++ templates are compile time
Upvotes: 1