masoud
masoud

Reputation: 56479

Why `expected primary-expression` when it seems ok

Following my previous question Protected member is unknown for derived class

I cannot understand which part of that line is wrong, Any idea?

There is a compile error here:

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();
}

Or, what's wrong with this statement:

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

The complete code is here, where you can test and compile it.

Upvotes: 1

Views: 87

Answers (2)

WhozCraig
WhozCraig

Reputation: 66194

You can resolve through the derived class to the base class like so (at least LLVM can =):

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();
}

Upvotes: 1

From looking at the complete code, I see edge is also defined in the base class. You must also tell the compiler it's a dependent name, like this:

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();

Upvotes: 5

Related Questions