c++ - 如何修复二元运算符错误?
问题描述
我必须编写使用邻接表来表示图形的代码。我尝试将我的代码设置为使用 std::list 但现在这给了我错误说 Error C2679 binary '=': no operator found which take a right-hand operand of type 'int' (或者没有可接受的转换)在我的代码的多行上。我对如何让我的代码使用邻接列表而没有所有这些问题感到困惑。
#include "GraphInterface.h"
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include<list>
#ifndef GRAPH_TWO
#define GRAPH_TWO
template<class LabelType>
class GraphTwo : public GraphInterface<LabelType>
{
private:
// Define maximum number of nodes
static const int size = 10;
std::list <int> adj[size][size];
std::list<bool>visited[size];
public:
GraphTwo();
// Get the number of vertices
int getNumVertices() const;
// Get the number of the edges
int getNumEdges() const;
// Creates an undirected edge in this graph between two vertices
// that have the given labels.If such vertices do not exist, creates
// themand adds them to the graph before creating the edge
bool add(LabelType start, LabelType end, int edgeWeight);
// Removes an edge from this graph. If a vertex has no other edges,
// it is removed from the graph since this is a connected graph.
bool remove(LabelType start, LabelType end);
// Gets the weight of an edge in this graph.
int getEdgeWeight(LabelType start, LabelType end) const;
// Performs a depth - first search of this graph beginning at the given
// vertex and calls a given function once for each vertex visited.
void depthFirstTraversal(LabelType start, void visit(LabelType&));
// Performs a breadth - first search of this graph beginning at the given
// vertex and calls a given function once for each vertex visited.
void breadthFirstTraversal(LabelType start, void visit(LabelType&));
};
template<class LabelType>
GraphTwo<LabelType>::GraphTwo()
{}
template<class LabelType>
int GraphTwo<LabelType>::getNumVertices() const
{
return size;
}
template<class LabelType>
int GraphTwo<LabelType>::getNumEdges() const
{
int edgeCount = 0;
for (int i = 0; i < size; ++i)
for (int j = 0; j < size; ++j)
if (adj[i][j] != 0)
++edgeCount;
return edgeCount / 2;
}
template<class LabelType>
bool GraphTwo<LabelType>::add(LabelType start, LabelType end, int edgeWeight)
{
adj[start][end] = edgeWeight; //error here
adj[end][start] = edgeWeight; //error here
return true;
}
template<class LabelType>
bool GraphTwo<LabelType>::remove(LabelType start, LabelType end)
{
adj[start][end] = 0; // error here
adj[end][start] = 0; // error here
return true;
}
template<class LabelType>
int GraphTwo<LabelType>::getEdgeWeight(LabelType start, LabelType end) const
{
return adj[start][end];
}
template<class LabelType>
void GraphTwo<LabelType>::depthFirstTraversal(LabelType start, void visit(LabelType&))
{
// Visit the current node
visit(start);
// Mark the current node as visited
visited[start] = true;
// For all other nodes
for (int i = 0; i < size; ++i) {
if (adj[start][i] != 0 && (!visited[i]))
depthFirstTraversal(i, visit);
}
}
template<class LabelType>
void GraphTwo<LabelType>::breadthFirstTraversal(LabelType start, void visit(LabelType&))
{
// Vector that contains the adjacent nodes
std::vector<LabelType> alist;
alist.push_back(start);
// Mark current node as visited
visited[start] = true; // error here
int check;
while (!alist.empty()) {
check = alist[0];
// Print node
visit(check);
alist.erase(alist.begin());
// Every vertex adjacent
for (int i = 0; i < size; ++i) {
if (adj[check][i] != 0 && (!visited[i])) {
// Add node to the queue
alist.push_back(i);
// Mark next node as visited
visited[i] = true; // error here
}
}
}
// Reset visited as all false
for (int i = 0; i < size; ++i)
visited[i] = false; // error here
}
#endif
解决方案
std::list <int> adj[size][size];
这不是你想的那样。adj
是大小列表的大小数组的数组。
这使得adj[start][end]
有类型std::list<int>
。这同样适用于您的所有列表。去找一个关于如何使用的好文档,std::list
不要试图猜测C++ 语法,学习它。
推荐阅读
- javascript - 通过多个异步 Axios 调用将预映射数据收集到解构器中
- c# - 如何在没有 ID 的情况下单击按钮?
- asp.net - 如何在 Entity Framework Core 中使用 VARCHAR 或 NVARCHAR 自动增加 Id?
- sql-server - SQL 数据库中的 dbo.geometry.columns 和 dbo.spatial_ref_sys 表是干什么用的?
- spring - 使用表达式语言获取值
- flutter - Flutter隐藏可搜索下拉按钮的下划线
- c++ - C ++无法返回已添加到向量列表中的所有变量
- python - Keras 模型不更新权重
- c# - 自定义列宽 OpenXml (excel)
- wordpress - (WordPress) - 查看/下载 PDF 时如何修改 htaccess 以使用 htpasswd 提示 passwd