我正在尝试创建一个图形,并且我正在尝试通过创建一组 Vertex 类型和一组 Edge 类型来做到这一点。但是,我收到此错误:
二进制表达式的无效操作数('const Vertex' 和 'const Vertex')
甚至可以将对象保存在集合中吗?我不是一个伟大的编码员,这是我的大学项目。如果您看到 ** 围绕代码块,那是因为我试图将这些部分加粗以尝试突出显示,但它不起作用。那些 ** 不在我的实际代码中。
class Vertex
{
public:
Vertex();
Vertex(std::string);
std::string getVertexName();
private:
std::string name;
};
class Edge{
Edge();
Edge(std::string, std::string);
std::string source;
std::string destination;
};
class Graph{
public:
void buildGraph( );
void createVertex(std::string vertexName);
// **
std::set <Vertex> setofvertices;
std::set <Vertex>::iterator itervertex = setofvertices.begin();
std::set <Edge> setofedges;
std::set <Edge>::iterator iteredge = setofedges.begin();
// **
};
Graph* DataSource:: buildGraph()
{
Graph *createdGraph;
//Vertex *createdVertex;
std::ifstream inputFile;
std::string followee, follower;
inputFile.open(this->filename);
if (inputFile.is_open()){
std::stringstream line;
std::string fileLine;
createdGraph = new Graph();
while(true){
getline(inputFile, fileLine);
if (inputFile.eof()) break;
line.clear();
line.str(fileLine);
line >> followee >> follower;
std::cout << followee << " " << follower << std::endl;
// **
createdGraph->setofvertices.insert(followee);
createdGraph->setofvertices.insert(follower);
createdGraph->setofedges.insert(followee,follower);
// **
}
return createdGraph;
}
最佳答案
错误是因为集合中的项目已排序并且 Vertex
和 Edge
不要为此提供机制。最简单的方法是实现 operator<
同时。
这是一个简单的示例,说明它的外观。我将成员公开,以便在最后打印更容易,但您可以将它们保密。
#include <iostream>
#include <set>
#include <string>
#include <tuple>
class Vertex
{
public:
Vertex(std::string name_)
: name(name_)
{}
bool operator<(const Vertex &rhs) const
{
return name < rhs.name;
}
std::string name;
};
class Edge
{
public:
Edge(std::string source_, std::string destination_)
: source(source_), destination(destination_)
{}
bool operator<(const Edge &rhs) const
{
// Easy way to handle comparison of multiple variables
// https://en.cppreference.com/w/cpp/utility/tuple/tie
return std::tie(source, destination) < std::tie(rhs.source, rhs.destination);
}
std::string source;
std::string destination;
};
class Graph
{
public:
std::set <Vertex> setofvertices;
std::set <Edge> setofedges;
};
int main()
{
Graph g;
g.setofvertices.emplace(Vertex("a"));
g.setofvertices.emplace(Vertex("b"));
g.setofvertices.emplace(Vertex("c"));
// intentional duplicate that will be rejected
g.setofvertices.emplace(Vertex("c"));
g.setofedges.emplace(Edge("a", "b"));
// intentional duplicate that will be rejected
g.setofedges.emplace(Edge("a", "b"));
g.setofedges.emplace(Edge("b", "c"));
g.setofedges.emplace(Edge("c", "a"));
for (auto &v : g.setofvertices)
{
std::cout << "Vertex: " << v.name << "\n";
}
for (auto &e : g.setofedges)
{
std::cout << "Edge : " << e.source << " -> " << e.destination << "\n";
}
}
您可以考虑替换此代码:
while(true){
getline(inputFile, fileLine);
if (inputFile.eof()) break;
只需
while (getline(inputFile, fileLine)) { ... }
.它与您现在拥有的相同,但更清晰,并且会捕获在读取不是 eof
时可能发生的任何其他错误.对于人们来说,这是一个常见的陷阱,他们在读取之前检查 eof,读取失败,然后他们使用错误数据进行处理,但是您完成它的方式,以及读取后的检查,消除了这个漏洞。
关于c++ - 如何将对象插入集合中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61578967/