本文介绍了派生类的受保护成员未知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Graphs找到了一个开源类库。当我把它包含在我的项目中有很多错误,我试图修复它们。但是有一个编译错误,我无法解决它。



基类:

 code> template< typename K,typename W,typename T> 
class _base_graph
{
// ...

protected:
std :: map< K,T&点头;
std :: list< edge> edg;
};

派生类:

  template< typename K,typename T = void *> 
class graph:public _base_graph< K,void *,T>
{
// ...
public:
void add_edge(const K& k1,const K& k2);
};

方法体:

  template< typename K,typename T> 
void graph< K,T> :: add_edge(const K& k1,const K& k2)
{
if(nod.find(k1)== nod.end | nod.find(k2)== nod.end())//< - error!
throw std :: string(add_edge:Node does not exist);

// ...
}

编译器显示一个错误:

You can find and test mycode in this online compiler.

解决方案

You need

this->nod.find(k2);

or

_base_graph<K, void*, T>::nod.find ....;

The base and the derived classes are templates, and in your code nod is a non-dependent name, and so is looked up at the point of graph's declaration. This is the first phase of the two-phase lookup. At this stage, it is impossible for the compiler (provided it follows the name lookup rules of the standard) to know what nod means, because it does not consider the base class until the second phase. So it is necessary to tell the compiler that nod should be looked up in the second phase. To do this, we explicitly tell it that nod is in a base class by using one of the forms above.

The reason for this behaviour is that at the point of the derived class' definition, it should not possible to know what _base_graph<K, void*, T>:: contains, to allow for specializations of the template that add and even hide names. So the trick above makes sure that the names are looked up at the point of the derived class' instantiation, when all the information is available.

In summary, there are two issues in play here:

  1. nod is not a dependent name, so it would be looked up in the first phase.
  2. The base class template, which declared nod, is not available until the second phase, so the name cannot be resolved.

By using this->nod or _base_graph<K, void*, T>::nod, we are explicitly dealing with a dependent name, forcing the lookup to take place in the second phase.

See points 7 to 10 here.

Thanks to @DavidRodriguez-dribeas for clarifying some of the finer points of the two phase look-up.

这篇关于派生类的受保护成员未知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:49