2013-10-25 33 views
0

我在一個圖類中工作,我剛開始爲頂點構建一個類,而另一個類爲邊,我的問題一般與圖形無關。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()); 
+0

哪些錯誤,具體是? – Ashalynd

+1

發佈錯誤消息,以便人們可以更好地幫助您。這就是說,它看起來像你試圖初始化first_vertex和second_vertex在構造函數的主體,而不是初始化列表。 –

回答

3

也許問題出在你的構造函數的語法:

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) 
{ 

} 

,你應該通過常量引用到頂點:

Edge(const Vertex& vertex_1, const Vertex& vertex_2, unsigned int init_weight) //constructor 
    : first_vertex(vertex_1.get_id()), second_vertex(vertex_2.get_id()), weight(init_weight) 
    { 
    } 

你試圖用它們的構造函數初始化你的頂點:

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

爲什麼我們必須將const引用傳遞給頂點..?提前致謝。 – mazlor

+1

在這種情況下,它不是必須的,但在類實例非常大的情況下,最好傳遞引用,以便不創建臨時副本。請參閱[this](http://stackoverflow.com/questions/2582797/why-pass-by-const-reference-instead-of-by-value)以獲取更多信息 – xorguy

0

也許這是修復:

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

不是解決此問題的首選方法。 –

+0

初始化程序更清潔,同意。 – Ashalynd

0

您正試圖以錯誤的方式設置在邊緣的頂點值。使用類似

first_vertex.set_id(vertex_1.get_id()); 

此外,這種類型的問題有標準的表示形式。查看更多在這裏:http://en.wikipedia.org/wiki/Graph_%28abstract_data_type%29#Representations

+0

它不是指向頂點的指針,它只是頂點。所以沒有新的運營商。 – Ashalynd

+0

他沒有在他的課堂上存儲'Vertex *',所以使用'new'是不合適的。 –

+0

你是絕對正確的。我分開了,並且正在考慮Java而不是C++。我更新了它。 – aring

1

hw2.cpp:32:41: error: no match for call to '(Vertex) (unsigned int)' first_vertex(vertex_1.get_id());

該錯誤消息如下處理在Edge構造函數體內(而不是在它的初始化列表中)。這不是有效的語法。您可能需要使用初始化列表(顯示,首選),或在構造函數的主體中使用它們的賦值運算符(語法正確,但不是首選,因爲Vertex將使用不正確的數據構造,然後進行初始化,而不是簡單地用正確的數據構造)。

一個更好的辦法是使用的Vertex拷貝構造函數(而不是轉換構造函數):

// notice the lack of calling the get_id function 
Edge(const Vertex& vertex_1, const Vertex& vertex_2, unsigned int init_weight) 
    : first_vertex(vertex_1), second_vertex(vertex_2), weight(init_weight) 
    { 
    } 

此外,與以下錯誤消息:

hw2.cpp:31:6: error: no matching function for call to 'Vertex::Vertex()

你有聲明瞭一個非默認的非複製構造函數(Vertex(unsigned int init_val)),因此編譯器不會爲您生成默認構造函數。由於當您嘗試在Edge構造函數的主體中初始化first_vertexsecond_vertex並且它不存在時,它將嘗試使用默認構造函數初始化Vertex,因此會出現錯誤。你可以通過聲明一個Vertex() {}構造函數來解決這個問題。

+0

爲什麼我們必須將const引用傳遞給頂點。 。?提前致謝。 – mazlor

+1

你不*有*,但它會更有效率,並且不會強制多次使用複製構造函數。你有兩個獨立的,但稍有關係的問題。 –