[英]Struct does not provide a subscript operator
I'm trying to loop through my adjacency list in my DFS search class, but its giving me this error: type 'AdjList' does not provide a subscript operator.我试图在我的 DFS 搜索类中遍历我的邻接列表,但它给了我这个错误:类型“AdjList”不提供下标运算符。 I feel like it might be the way i saved adjList constructor for DFS but I'm not sure.我觉得这可能是我为 DFS 保存 adjList 构造函数的方式,但我不确定。 I saw other solutions saw it has to be passed as a pointer so I changed AdjList list
in the constructor to AdjList* list
but that did not work.我看到其他的解决办法看到它作为一个指针传递,所以我改变AdjList list
在构造函数中AdjList* list
,但没有奏效。 Here is my code:这是我的代码:
main.cpp主程序
AdjList::AdjList(){}
AdjList::AdjList(vector<Node> nodeVector){
nodeContainer = nodeVector;
}
void AdjList::makeAdjList(){
int temp;
for(int i = 0; i < nodeContainer.size(); i++){
connections = nodeContainer[i].getConnectionsVector();
for(int x = 0; x < connections.size(); x++){
innerList.push_back(nodeContainer[connections[x] - 1]);
}
adjList.push_back(innerList);
innerList.clear();
}
}
//other AdjList functions
int main(){
vector<Node> nodeContainer;
nodeContainer = load();
AdjList adjList(nodeContainer);
adjList.makeAdjList();
DFS search(nodeContainer, adjList);
search.iterative(6,3);
}
DFS.cpp DFS文件
DFS::DFS(vector<Node> nodeVec, AdjList list){
nodeContainer = nodeVec;
adjList = list;
}
vector<Node> DFS::iterative(int src, int dest){
vector<Node> vectorPath;
list<Node>::iterator it;
int i = 6;
for(it = adjList[i].begin(); it != adjList[i].end(); it++){ //this is where the
//error is happening
cout << it->getNodeID() << " ";
}
return vectorPath;
}
AdjList.h调整列表.h
class AdjList{
private:
public:
list<Node> innerList;
vector<list<Node> > adjList;
vector<Node> nodeContainer;
vector<int> connections;
int temp;
AdjList();
AdjList(vector<Node> nodeVector);
void makeAdjList();
void displayAdjList();
};
When you say adjList[i]
, C++ looks for an operator[]
in the type of adjList
which is AdjList
.当你说adjList[i]
,C++ 会在adjList
的类型中寻找一个operator[]
,它是AdjList
。 As this type doesn't have such a member function you get the error message that it's missing.由于这种类型没有这样的成员函数,您会收到错误消息,指出它丢失了。
Supply a供应一个
std::list<Node>& operator[](std::size_t i);
std::list<Node> const& operator[](std::size_t i) const;
and return adjList[i]
in both.并在两者中返回adjList[i]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.