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::
(I can see it useful if it were static member function, in which case there is no point (?) in inheriting it )
#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##### Gives error when just Write();
}
};
int main()
{
Writer<BoldPen> writer1;
writer1.StartWriting();
return 0;
}
EDIT:
Although the linked question (duplicate of) does answer the core reason behind it, I am more concerned with its effect on member functions after the inheritance is completed. Particularly, the syntax used is quite similar to how one would access static methods. Does that go well with dependent name lookup?Also see the comments for further info. Thanks.