当看起来好的时候,为什么“期望初级表达”

时间:2012-11-16 10:04:16

标签: c++

按照我之前的问题 Protected member is unknown for derived class

我无法理解那条线的哪一部分是错的,不知道吗?

这里有编译错误:

template <typename K, typename T>
bool graph<K, T>::is_edge(const K& k1, const K& k2)
{
  if (this->nod.find(k1) == this->nod.end() || this->nod.find(k2) == this->nod.end())
    throw std::string("is_edge: Node does not exist");

  if (k1 < k2) // Below line makes error: expected primary-expression!!!!
    return std::find(this->edg.begin(), this->edg.end(), edge(k1, k2)) != this->edg.end();
  return std::find(this->edg.begin(), this->edg.end(), edge(k2, k1)) != this->edg.end();
}

或者,这句话有什么问题:

std::find(this->edg.begin(), this->edg.end(), edge(k1, k2)) != this->edg.end();

完整的代码是here,您可以在其中测试和编译它。

2 个答案:

答案 0 :(得分:5)

从查看完整代码,我看到edge也在基类中定义。您还必须告诉编译器它是一个依赖名称,如下所示:

if (k1 < k2) // Below line makes error: expected primary-expression!!!!
  return std::find(this->edg.begin(), this->edg.end(), typename _base_graph<K, void*, T>::edge(k1, k2)) != this->edg.end();
return std::find(this->edg.begin(), this->edg.end(), typename _base_graph<K, void*, T>::edge(k2, k1)) != this->edg.end();

答案 1 :(得分:1)

您可以通过派生类将其解析为基类(至少LLVM可以=):

template <typename K, typename T>
bool graph<K, T>::is_edge(const K& k1, const K& k2)
{
    typedef typename graph::edge edge;

    if (this->nod.find(k1) == this->nod.end() || this->nod.find(k2) == this->nod.end())
        throw std::string("is_edge: Node does not exist");

    if (k1 < k2)
        return std::find(this->edg.begin(), this->edg.end(), edge(k1, k2)) != this->edg.end();
    return std::find(this->edg.begin(), this->edg.end(), edge(k2, k1)) != this->edg.end();
}