It cannot find it because both name
and description
return a Builder&
instead of a WeaponBuilder&
, hence those other methods are not there. There is no clear solution for your code, other than casting everywhere.
You could rewrite the entire thing using CRTP and get ride of your problems, but its a significant change. Something among the lines of:
template< typename Derived >
class builder
{
Derived& name( std::string const& name ){ /*store name*/, return *derived(); }
Derived* derived(){ return static_cast< Derived* >( this ); }
};
class weapon_builder : builder< weapon_builder >
{
weapon_builder& attack( int ){ /*store attack*/ return *this; }
GameComponent* build() const{ return something; }
};
Note that with this approach all virtual
methods should go away, and you lose the ability to reference a plain builder
as it is no longer a common base type but a class template.