我在一個圖類中工作,我剛開始爲頂點構建一個類,而另一個類爲邊,我的問題一般與圖形無關。C++中的圖形/邊緣類構造函數
首先我建立一個類名叫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;
}
添加構造封邊代碼在運行時不出現錯誤,將試圖運行代碼時,我收到錯誤的構造邊緣前後,我不能夠確定我以上錯誤。
感謝您的建議。
這裏是錯誤消息我收到:
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());
哪些錯誤,具體是? – Ashalynd
發佈錯誤消息,以便人們可以更好地幫助您。這就是說,它看起來像你試圖初始化first_vertex和second_vertex在構造函數的主體,而不是初始化列表。 –