c++ - C++ 中的图/边类构造函数

标签 c++ graph

我在图类中工作,我刚刚开始为顶点构建一个类,为边构建另一个类,我的问题一般与图无关。

首先我构建了一个名为 Vertex 的类,到目前为止我在实现它时没有遇到任何问题,然后我开始了另一个名为 Edge 的类,Edge 有三个主要成员,其中两个的类型为 Vertex ,并且第三个成员的类型为 unsigned int。

代码如下:

#include<iostream>
using namespace std;



class Vertex
{
 private:
     unsigned int id;                                 
 public:   
    unsigned int get_id(){return id;};   
    void set_id(unsigned int value) {id = value;};
    Vertex(unsigned int init_val) {id = init_val;};   
    ~Vertex() {};                                     
};


class Edge
{
 private:
      Vertex first_vertex;                 // a vertex on one side of the edge
      Vertex second_vertex;                // a vertex on the other side of the edge
      unsigned int weight;                 // the value of the edge ( or its weight )     
 public:   
    Edge(Vertex vertex_1, Vertex vertex_2, unsigned int init_weight)  //constructor
    {
         first_vertex(vertex_1.get_id());
         second_vertex(vertex_2.get_id());
         weight = init_weight;
      }

    ~ Edge();   // destructor
}; 

///////////////////////////////// this part is to test the result

Vertex ver_list[2] = {7, 9};
Vertex test = 101;

int main()
{
    cout<< "Hello, This is a graph"<< endl;
    for (unsigned int i = 0; i < 2; i++) cout<< ver_list[i].get_id() << endl;      
    cout<< test.get_id() << endl;

return 0;
}

在添加构造函数 Edge 之前,代码运行没有错误,在添加构造函数 Edge 之后,我在尝试运行代码时收到错误,我无法确定我上面的错误。

感谢您的建议。

这是我收到的错误消息:

hw2.cpp: In constructor 'Edge::Edge(Vertex, Vertex, unsigned int)':
hw2.cpp:31:6: error: no matching function for call to 'Vertex::Vertex()'
      {
      ^
hw2.cpp:31:6: note: candidates are:
hw2.cpp:13:2: note: Vertex::Vertex(unsigned int)
  Vertex(unsigned int init_val) {id = init_val;};   // constructor
  ^
hw2.cpp:13:2: note:   candidate expects 1 argument, 0 provided
hw2.cpp:6:7: note: Vertex::Vertex(const Vertex&)
 class Vertex
       ^
hw2.cpp:6:7: note:   candidate expects 1 argument, 0 provided
hw2.cpp:31:6: error: no matching function for call to 'Vertex::Vertex()'
      {
      ^
hw2.cpp:31:6: note: candidates are:
hw2.cpp:13:2: note: Vertex::Vertex(unsigned int)
  Vertex(unsigned int init_val) {id = init_val;};   // constructor
  ^
hw2.cpp:13:2: note:   candidate expects 1 argument, 0 provided
hw2.cpp:6:7: note: Vertex::Vertex(const Vertex&)
 class Vertex
       ^
hw2.cpp:6:7: note:   candidate expects 1 argument, 0 provided
hw2.cpp:32:41: error: no match for call to '(Vertex) (unsigned int)'
           first_vertex(vertex_1.get_id());
                                         ^
hw2.cpp:33:42: error: no match for call to '(Vertex) (unsigned int)'
           second_vertex(vertex_2.get_id());

最佳答案

也许问题出在构造函数中的语法:

Edge(Vertex vertex_1, Vertex vertex_2, unsigned int init_weight)  //constructor
{
         first_vertex(vertex_1.get_id());
         second_vertex(vertex_2.get_id());
         weight = init_weight;
}

您应该在初始化列表中初始化成员。

Edge(Vertex vertex_1, Vertex vertex_2, unsigned int init_weight) : first_vertex(vertex_1.get_id()), second_vertex(vertex_2.get_id()), weight(init_weight)
{

}

并且您应该将 const 引用传递给 Vertex:

Edge(const Vertex& vertex_1, const Vertex& vertex_2, unsigned int init_weight)

关于c++ - C++ 中的图/边类构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19595917/

相关文章:

java - 如何在 Dijkstra 最短路径上获取路径

r - R 中多种图形类型的对齐(最好在 ggplot2 或 lattice 中)

python - 在python中读取图中的相邻元素

c++ - 我的 Floyd-Warshall C++ 实现中的错误

c++ - 仅使用指针和两个非指针变量进行交换?

c++ - 错误 C2664 : cannot convert argument 1 from 'NvPhysicalGpuHandle'

c++ - 对象销毁时内存泄漏

c++ - 在 OpenGL 中删除部分绘图

python - 如何在 matplotlib 饼图中显示实际值

c++ - 我无法正确声明这些变量...为什么这些参数不起作用?我只是将 n 值设置为数组长度