所以我试图重载一个<<我的类的运算符使用friend关键字访问私有字段。 代码:
//Edge.h
#pragma once
class Edge
{
private:
int startVertice;
int endVertice;
int weight;
public:
//cut
friend std::ostream& operator<< (std::ostream&, const Edge*);
};
//Edge.cpp
//cut
std::ostream& operator<< (std::ostream& out,const Edge *n) {
out << "Start: [" << n->startVertice << "] End: [" << n->endVertice << "] Weight: [" << n->weight << "]";
return out;
}
//cut
然而,IntelliSense告诉我member Edge::startVertice is inaccessible
。 endVertice
和weight
也是如此。
我哪里弄错了? :d
编辑: 编译器错误(与此案例相关):