带有"operator="和向量的c ++类,我不知道如何正确使用它
c++ class with 'operator=' and vector, I don't know how to use it correctly
我尝试在特定顶点中的向量(边缘)的特定边缘上使用operator=。 但是我得到了这个错误:
source_file.cpp:在成员函数中 'void Graph::addEdges(int, int, int, int)': source_file.cpp:43:26: 错误: 与 'operator=' 不匹配 (操作数类型为"std::vector"和"Edge")
arr[source].arr2[dest] = Edge(source, dest, a, b);
#include <iostream>
#include <string>
#include <queue>
using namespace std;
const int maxVer = 60;
struct Edge{
int source;
int dest;
int a;
int b;
public:
Edge() : a(0), b(0){};
Edge(int source, int dest, int a, int b) : source(source), dest(dest), a(a), b(b) {}
Edge& operator=(const Edge& e){
source = e.source;
dest = e.dest;
a = e.a;
b = e.b;
return *this;
}
};
struct Vertex{
int parent;
int dest;
bool visited;
int numberEdges;
string str;
vector<Edge> arr2[maxVer-1];
};
class Graph{
int vertexNumber=0;
Vertex arr[maxVer];
queue<int> que;
public:
void addEdges(int source, int dest, int a, int b){
arr[source].arr2[dest] = Edge(source, dest, a, b);
arr[dest].arr2[source] = Edge(dest, source, b, a);
++arr[source].numberEdges;
++arr[dest].numberEdges;
}
};
int main(){
return 0;
}
问题出在哪里?我该如何解决它?
谢谢!
当你这样做时在你的Vertex
类中
vector<Edge> arr2[maxVer-1];
您不会声明一个大小为maxVer-1
个元素的向量,而是声明一个包含maxVer-1
个向量的数组。
如果你想要一个向量,那么做:
struct Vertex
{
// Declare the vector
vector<Edge> arr2;
// ... all other member variables...
// Add constructor with a member initializer list
Vertex()
: arr2(maxVer-1) // Initializes the vector to have maxVer-1 elements
{}
};
请注意更改后的顺序,其中首先声明arr2
。如果不是,则必须在构造函数成员初始值设定项列表中添加所有其他成员的初始化。
相关文章:
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 我正在尝试使用 c++ 创建一个货币转换程序,我不知道如何继续
- 我不知道这条线是做什么的
- 如果我不知道每个列表中有多少个数字,我如何将给定数量的数字列表作为输入?
- 我不知道导致错误的原因 (C3074)
- 我不知道为什么这段代码会让核心被转储?
- 我正在尝试制作一个自平衡机器人,但编译时存在错误。我不知道如何解决它
- 我不知道如何使用代码块win32 gui
- 没有函数模板的实例与我不知道为什么的参数列表匹配
- if-else 语句仅按特定顺序工作,我不知道为什么
- DirectX 11 引擎在第三次代码改进后没有突然运行,我不知道为什么
- 程序输入名称和高度并显示它。我不知道如何显示列表中最高人的姓名和身高
- 函数抛出seg错误,我不知道为什么
- 我不知道为什么复制构造函数的调用在 c++ 中不稳定
- 我已经做了一个程序来使用队列链C++表显示学生姓名和身高,现在我想以我不知道如何显示的顺序显示它
- 我看过Stellarium代码,我不知道它从哪里开始
- 带有"operator="和向量的c ++类,我不知道如何正确使用它
- 我不知道如何正确地将数据输入到地图中
- 在我的 7 位加法器中,进位不正确,我不知道为什么