I don't get that compiler error in VC9. However, there are several problems with the code: First, it doesn't need to be a template class as it's currently written...but maybe you just simplified it for this question? Second, The base class should have a virtual destructor.
#include <iostream>
using namespace std;
class class0 {
public:
virtual ~class0(){}
protected:
char p;
public:
char getChar();
};
class class1 : public class0 {
public:
void printChar();
};
void class1::printChar(){
cout << p << endl;//p was not declared in this scope
}
int main() {
class1 c;
c.printChar();
return 1;
}
Since you're learning about templates, I would suggest not mixing concepts (inheritance & templates) while learning. Start with a simple example like this...
#include <iostream>
#include <string>
using namespace std;
template <typename T>
T add(const T& a, const T& b) {
return a + b;
}
int main() {
int x = 5;
int y = 5;
int z = add(x, y);
cout << z << endl;
string s1("Hello, ");
string s2("World!");
string s3 = add(s1, s2);
cout << s3 << endl;
return 1;
}
The important concept in the code above is that we wrote ONE function that knows how to add integers and strings (and many other types for that matter).