【问题标题】:Array of STL List not accessing List functionsSTL 列表数组未访问列表函数
【发布时间】:2014-04-01 00:07:15
【问题描述】:

我有一个列表数组:

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)

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

编辑:在我的课堂上,我有: int* adj;

然后在我的一个函数中,在我从用户那里获得数组的大小后,我有 std::list&lt;int&gt; 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);

【问题讨论】:

  • 去掉int* adj;?为什么你还有那个?
  • "我有一个列表数组:" 你有一个重复的标识符编译错误。我会先解决这个问题。 adj 被多次定义。
  • “列表数组:”你能显示所提到的数据结构的完整声明吗?

标签: c++ arrays list stl


【解决方案1】:

您正在尝试访问 int 上的成员方法 size()

int* adj;

您已为变量 adj 重新定义(或未定义列表)。编译器认为您在谈论 int* adj 而不是 std::list&lt;int&gt; 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();

【讨论】:

  • int* adj 在我的班级的私有中声明。如果我在私有中使用第二个定义,我不知道编译时 n 会是什么。有没有办法解决这个问题?
  • @Walter - 进行了一些编辑,希望能为您解决问题。
  • @Walter 为了与这种方法保持一致,这意味着创建一个setListSize(int indexPosition, int listLen) 方法或类似名称的公共成员函数来为列表向量中的每个列表传递listLen 值。或者,如果为每个列表的元素数量设置预定值并不重要,则根据需要将元素添加/删除到适当的列表中。
  • 我现在似乎遇到的问题是,当我使用这个方法时,在我的代码中到处都是它告诉我 AdjVec 没有被声明。
  • @Walter 新的 AdjVec 实例是 GenericClass 定义的一部分(例如私有访问)?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-17
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 2020-11-19
相关资源
最近更新 更多