As shown in the code, I am inheriting the class BoldPen
, obtained from the template parameter to another class Writer
, and creating writer1
object.
My question is regarding the 'line of interest' marked in the code below. As it stands, the code compiles fine and works. However, if I remove the PenType::
scope, changing the line to just Write
the code fails compilation with error
main.cpp: In member function ‘void Writer<PenType>::StartWriting()’:
main.cpp:17:11: error: there are no arguments to ‘Write’ that depend on a template parameter, so a declaration of ‘Write’ must be available [-fpermissive]
Write();
^
From my understanding, shouldn't it compile fine since the BoldPen
class is being inherited by Writer
, which means it should see the Write
method in its superclass BoldPen
? Why is PenType::
required
#include <iostream>
struct BoldPen
{
void Write()
{
std::cout << "Writing using a boldpen" << std::endl;
}
};
template<class PenType>
class Writer : public PenType
{
public:
void StartWriting()
{
PenType::Write(); //#####LINE OF INTEREST#####//
}
};
int main()
{
Writer<BoldPen> writer1;
writer1.StartWriting();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire