我有一个列表数组:
int* adj;
std::list<int> adj[n];//where n is the size of the array
我的问题是当我需要adj[v].size()
其中 v 是我当前所在的索引,我收到错误:
request for member 'size' in '((GenericClass*)this)->GenericClass::adj', which is of non-class type 'int*' for(int i=0; i<adj.size(); ++i)
对于我尝试在 STL 列表类中访问的每个其他函数,我同样遇到了这个问题。我也尝试创建一个迭代器:
for(std::list<int>::iterator it=adj[v].begin(); it != adj[v].end(); ++it)
但我遇到了与之前所述相同的问题。
编辑:在我的类里面,我有: int* 调整;
然后在我的一个函数中,在我从用户那里得到数组的大小之后,我有
std::list<int> adj[n]
行。
编辑 2:
我现在已经将我的私有(private)更改为:typedef std::list<int> IntList;
typedef std::vector<IntList> AdjVec;
AdjVec adj;
我在我的 public 中有一个函数,int GenericClass::search(AdjVec adj, int v) 我得到一个错误
'AdjVec' has not been declared
int search(AdjVec adj, int v);
^
GenericClass.cc:234:20: error: no matching function for call to 'GenericClass::search(GenericClass::AdjVec&, int&)'
u= search(adj, v);
最佳答案
您正在尝试访问成员方法 size()
在 int
上.
int* adj;
您已为变量 adj
重新定义(或未定义列表) .编译器认为你在谈论 int* adj
而不是 std::list<int> adj[n];
摆脱第一个定义并使用第二个。
编辑:
好像你不知道什么n
将在编译时,并且 adj
是您类(class)之一的成员。在这种情况下,只需使用 vector
并在运行时动态调整它的大小。
// In your header.
typedef std::list<int> IntList;
typedef std::vector<IntList> AdjVec;
AdjVec adj;
// In your cpp, when you know what 'n' is.
adj.resize(n);
adj[0].size();
关于c++ - 不访问列表函数的 STL 列表数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22774501/