STL 列表数组无法访问列表函数

Array of STL List not accessing List functions

本文关键字:列表 访问 函数 数组 STL      更新时间:2023-10-16

我有一个列表数组:

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 List 类中访问的每个其他函数,我同样会遇到这个问题。我也尝试创建一个迭代器:

 for(std::list<int>::iterator it=adj[v].begin(); it != adj[v].end(); ++it)

但我遇到了与之前所说的相同的问题。

编辑:在我的班级的私人中,我有:国际* 调整;

然后在我的一个函数中,在我从用户那里获得数组的大小后,我有std::list<int> adj[n]行。

编辑2:

我现在已经把我的私人改为阅读:typedef std::list<int> IntList; typedef std::vector<IntList> AdjVec; AdjVec adj;

我在我的公共中有一个函数,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);

您正在尝试访问intsize()的成员方法。

int* adj;

您已为变量 adj 重新定义(或未定义列表)。编译器认为你在谈论int* adj而不是std::list<int> adj[n];

摆脱第一个定义并使用第二个定义。

编辑:似乎您不知道编译时n是什么,并且adj是您的一个类的成员。在这种情况下,只需使用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();