2012-02-26 56 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