Protected member of base class is not accessable from derived class

I am unable to understand why my program is not running
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
#include <iostream>
#include<typeinfo>
using namespace std;

template<class T>
class num
{
protected:
    T val;
public:
    num(T x):val(x){}
    virtual T getval() {return val;}
};
template<class T> class sqrnum:public num<T>
{
public:
    sqrnum(T x):num<T>(x){}
    T getval(){return val*val;}
};
int main()
{
    num<int> *bp, num_int(2);
    sqrnum<int> *dp, sqr_int(3);
    num<double> *bpd,num_double(1.2);
    bp=dynamic_cast<num<int>*>(&sqr_int);
    cout<<typeid(*bp).name()<<endl;
    return 0;
}
Sorry,I am.still unable to solve the problem.Will you please solve the error for me? It will help me a lot
Change line 18 as follows (inform the compiler that val is a dependent name):
1
2
//    T getval(){return val*val;}
T getval() { return num<T>::val * this->val ; }
Topic archived. No new replies allowed.