c++ - 不访问列表函数的 STL 列表数组

标签 c++ arrays list stl

我有一个列表数组:

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/

相关文章:

c++ - 为什么与 tcmalloc 链接会减少 500MB 的内存使用量?

list - 两个列表的算术 Prolog

c++ - 如何在 C++ 列表中保存对象(在本例中为玩家)?

php - JQuery .post - $_POST 为空

python - 从函数列表中返回项目。 Python

c++ - MSVC12 认为从 std::array 派生的聚合不是 pod

c++ - C++Driver中Blob类型的映射

c++ - 如何将新节点插入到单链表中,我们没有指针指向它的头部?

javascript - Angular 交互 2 种不同类型的数组 - 单个 ng-repeat 中的线性数组和 json 数组

arrays - 德尔福。多维数组作为参数