"expression cannot be used as a function" 在 return 语句中

"expression cannot be used as a function" in return statement

代码(也 screenshot):

/***
Attempts to emplace new_vert in vertices,
    returning false
        if it failed (i.e., the vertex was already in vertices)
    or true
        if it succeeded
***/
template <typename T>
bool Graph<T>::addVert(const T& new_vert) {
    return vertices.emplace(std::piecewise_construct,
        std::forward_as_tuple(new_vert),
        std::forward_as_tuple()).second();
};

如截图所示,错误出现在“.second()”的最后一个括号处。

verticesstd::map<T, std::set<T>>emplace() should return a pair<iterator,bool> 类型,布尔值可通过 second() 访问,因此返回它。代码的不同版本(例如,用变量 foo 捕获对并返回 foo.second())没有区别;错误跟在 second().

的最后一个括号之后

看看有这个错误的其他问题,它们似乎归结为运算符在某处丢失,但这里似乎并非如此。为什么会这样?

The second member of std::pair是成员变量,不是成员函数。您只需要省略括号,因为您正在获取它的值(而不是调用它):

return vertices.emplace(std::piecewise_construct,
    std::forward_as_tuple(new_vert),
    std::forward_as_tuple()).second;