C++ 朋友不';t在C+中显示为公共+;

C++ 朋友不';t在C+中显示为公共+;,c++,class,private,public,C++,Class,Private,Public,我和一个朋友在公共部分写了一个类: class Graph { // ... snip ... public: Graph()= default; friend Graph operator+ (const Graph &g1, const Graph &g2); }; 但当我在main.cpp中编写以下内容时: Graph g{}; g. 我的IDE没有显示操作符+作为可能的完成 毕竟,operator+不是公共的吗?发生了什么?g.将使您的IDE

我和一个朋友在公共部分写了一个类:

class Graph {
    // ... snip ...

public:
    Graph()= default;
    friend Graph operator+ (const Graph &g1, const Graph &g2);
};
但当我在main.cpp中编写以下内容时:

Graph g{};
g.
我的IDE没有显示
操作符+
作为可能的完成


毕竟,
operator+
不是公共的吗?发生了什么?

g.
将使您的IDE显示
图形
成员函数

这是一个自由(非成员)函数:

friend Graph operator+ (const Graph &g1, const Graph &g2);
如果您添加一个成员函数,如下面的
operator+=
,则可能会显示:

class Graph {
private:
    // ... snip ...

public:
    Graph() = default;
    Graph& operator+=(const Graph& rhs);                      // this may show up
    friend Graph operator+(const Graph &g1, const Graph &g2);
};

Graph& Graph::operator+=(const Graph& rhs) {
   // ... impl ...
   return *this;
}

Graph operator+(const Graph &g1, const Graph &g2) {
    Graph retval(g1);
    return retval += g2;
}

注意:通过使用成员函数
operator+=
operator+
中进行上述操作,自由函数将不再需要成为
friend

为什么要进行否决?我想是因为几乎完全缺乏细节。您的问题是不可重现的。恰当地陈述问题。例如,什么弹出窗口?C++中没有弹出窗口。至少在我发表了以上评论后,您已经发布了该类。现在你可以添加你使用的IDE。我认为这里有一个合法的C++问题,它只是用未命名的IDE来表述的。