When I was implementing a data structure, I encountered the following problem of use of undeclared identifier 'a'. Isn't an inherited from the base class? Why can't I access it?
#pragma once
#include <iostream>
template <typename T> class A {
public:
T a;
virtual void show() = 0;
};
template <typename T> class B : public A<T> {
public:
B() { a = 0; }
void show() { std::cout << "Hello, world!" << std::endl; }
};
But when I added the this pointer, there was no error message:
#pragma once
#include <iostream>
template <typename T> class A {
public:
T a;
virtual void show() = 0;
};
template <typename T> class B : public A<T> {
public:
B() { this->a = 0; }
void show() { std::cout << "Hello, world!" << std::endl; }
};
Why???