我在一个图形类中工作,我刚刚开始为顶点构建一个类,为边缘构建另一个类,我的问题一般与图形无关。
首先我建立了一个名为 Vertex 的类,到目前为止我在实现它时没有遇到任何问题,然后我开始了另一个名为 Edge 的类,Edge 有三个主要成员,其中两个是 Vertex 类型,第三个成员有无符号整数类型。
这是代码:
#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());