2012-02-26 34 views
0

爲了讓這也是加權重圖,我以後的事數據結構,重圖

#include <iostream> 
#include <vector> 

using namespace std; 

struct maps 
{ 
    vector<char> weight(10); //to store weight of self-loops and multi-edges 
}; 

int main() 
{ 
    maps m1[101][101], m2[101][101]; 

    return 0; 
} 

,但我得到以下錯誤:

error: expected identifier before numeric constant 
error: expected ‘,’ or ‘...’ before numeric constant 

我該如何解決這個問題?

+1

'vector weight;'。不要在'struct'定義中啓動成員。它們應該在構造函數中或之後啓動。 – 2012-02-26 11:12:38

+0

@AdeYU:我不明白 – 2012-02-26 11:25:28

回答

3

正如Ade YU所說,不要在聲明中定義你的權重向量的大小。相反,在構造函數的初始化列表中執行它。這應該做你正在尋找的東西:

#include <iostream> 
#include <vector> 

using namespace std; 

struct maps 
{ 
    maps() : weight(10) {} 
    vector<char> weight; //to store weight of self-loops and multi-edges 
}; 

int main() 
{ 
    maps m1[101][101], m2[101][101]; 

    return 0; 
} 
+0

我想要一個大小爲10的重量矢量。怎麼做? – 2012-02-26 11:28:56

+0

在構造函數中,它將矢量初始化爲大小10. – 2012-02-26 11:32:30

+0

大小爲10的矢量有助於存儲可能存在於圖形中的邊緣的各種值/權重。 – 2012-02-26 11:36:56

0

你需要在構造函數中初始化向量。試試這個:

#include <iostream> 
#include <vector> 
using namespace std; 
struct maps{ 
    maps() : weight(10) {} 
    vector<char> weight;//to store weight of self-loops and multi-edges 
}; 

int main() 
{ 
    maps m1[101][101], m2[101][101]; 
    return 0; 
} 
+0

爲什麼這樣做? – 2012-02-26 11:39:56

+0

聲明類成員時,不能調用構造函數。你需要在類的構造函數中明確地調用它。從C++的角度來看,結構和類只有它們的默認訪問權限(公有與私有)不同。 – 2012-02-26 11:44:40