gpt4 book ai didi

c++ - 编写图形类时出现此错误是什么意思?

转载 作者:行者123 更新时间:2023-11-30 05:39:53 25 4
gpt4 key购买 nike

我有一个不完整的图表,其中有很多功能尚未实现。

这是一个相邻列表的图表。它有顶点和边。我收到一个我无法理解的错误。您能解释一下错误是如何产生的吗?错误是

C:\Users\jialee\Documents\CodeBlocks\ShortestPath\Graph.cpp: In constructor 'Edge::Edge(Vertex, Weight)': C:\Users\jialee\Documents\CodeBlocks\ShortestPath\Graph.cpp:34:33: error: no matching function for call to 'Vertex::Vertex()'

代码是

#include <forward_list>
#include <string>

using namespace std;

const int MAX_SIZE = 10000;
typedef int Weight;

class Vertex {
public:
Vertex(string name, int num);
string city_name;
int city_num;
};

class Edge{
public:
Edge(Vertex v, Weight w);
Vertex associated_vertex;
Weight weight;
};

class Graph{
public:
Graph(int size);
};

Vertex::Vertex(string name, int num){
city_name = name;
city_num = num;
}

Edge::Edge(Vertex v, Weight cost){
associated_vertex = v;
weight = cost;
}

Graph::Graph(int size = MAX_SIZE){
forward_list<Edge> G[size];
}

最佳答案

错误表明您缺少 Vertex 的默认构造函数(没有参数的构造函数),这是在构造边时所必需的。
基本上,您的边缘构造函数尝试首先默认初始化所有成员,然后分配传递的值。

您可以将默认构造函数添加到您的顶点类或(更好)在您的边构造函数中使用初始化列表:

Edge::Edge(Vertex v, Weight cost):
associated_vertex{v},
weight{cost}
{ }

关于c++ - 编写图形类时出现此错误是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32040864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com