Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 结构不提供下标运算符_C++ - Fatal编程技术网

C++ 结构不提供下标运算符

C++ 结构不提供下标运算符,c++,C++,我试图在DFS搜索类中循环遍历我的邻接列表,但它给了我以下错误:类型“AdjList”不提供下标运算符。我觉得这可能是我为DFS保存adjList构造函数的方式,但我不确定。我看到了其他解决方案,认为它必须作为指针传递,所以我将构造函数中的AdjList list更改为AdjList*list,但这不起作用。这是我的密码: main.cpp AdjList::AdjList(){} AdjList::AdjList(vector<Node> nodeVector)

我试图在DFS搜索类中循环遍历我的邻接列表,但它给了我以下错误:类型“AdjList”不提供下标运算符。我觉得这可能是我为DFS保存adjList构造函数的方式,但我不确定。我看到了其他解决方案,认为它必须作为指针传递,所以我将构造函数中的
AdjList list
更改为
AdjList*list
,但这不起作用。这是我的密码:

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::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::AdjList(){}
AdjList::AdjList(向量节点向量){
nodeContainer=nodeVector;
}
void AdjList::makeAdjList(){
内部温度;
对于(int i=0;i
DFS.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::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;
    }
DFS::DFS(向量节点、调整列表){ nodeContainer=nodeVec; adjList=列表; } 向量DFS::迭代(int src,int dest){ 矢量路径; 列表::迭代器; int i=6; 对于(it=adjList[i].begin();it!=adjList[i].end();it++){//这是 //错误正在发生
当您说“代码> AdjList[i]/Cuth>时,C++在<代码> > AdjList类型中查找< <代码>运算符[]/COD> >代码> AdjList< /Cord>。由于该类型没有这样的成员函数,您会得到它丢失的错误信息。

供应

std::list<Node>& operator[](std::size_t i);
std::list<Node> const& operator[](std::size_t i) const;
std::list&operator[](std::size\u t i);
标准::列表常数和运算符[](标准::大小i)常数;

然后返回
adjList[i]

类的定义是什么
adjList
?它是否有
[]
操作符和
begin()
方法?它的错误消息是什么?我将它添加到帖子中了。那么我如何解决这个问题呢?因为adjList类型实际上是由一个向量组成的,我应该能够使用这个操作符[]@CrashMan123请参见编辑。您的类中有哪些成员并不重要。运算符不会神奇地添加。您可以说
adjList.adjList[i]
,顺便说一句。很好,我实现了它,它成功了。这叫什么,为什么它有效?@CrashMan123看一看。你在这里实现的是一个自定义下标操作符,它允许类
AdjList
的对象
x
被下标为
std::size\u t
,例如:
x[I]
。您可能会感到困惑的是,外部作用域中的对象名为
adjList
,它与您的成员标识符一致。可能值得考虑重命名该成员。此外,您正在重载
运算符[]
成员函数,以便可以对
常量
对象以及可变对象调用它。