gpt4 book ai didi

c++ - 使用迭代器的编译器错误 : "error: ‘...::iterator’ has no member named '...' "

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:23:04 25 4
gpt4 key购买 nike

下面是代码。更多相关位位于 Class Edge 和 Vertex 下,错误抛出复制构造函数在初始 block 下方。我知道这肯定与我调用结束和开始的方式有关,因为它们在 Edge() 类中是 Vertex* 类型的,并且我用 std::类型的迭代器调用它们 vector::边缘*>。这是基于指针的东西。是的,这是针对类的,但这是一个包含更多内容的类项目,这只是我遇到的一个错误。提前致谢,我可能只是因为 sleep 不足或其他原因而看不到明显的东西。

#ifndef GRAPH_H
#define GRAPH_H

#include <vector>
#include <limits>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

template <typename Object, typename Weight>
class graph {
public:

class Vertex;

class Edge {
public:
Edge(Vertex* v, Vertex* w, Weight setweight) {
start = v;
end = w;
v->edge.push_back(this);
w->inedge.push_back(this);
weight = setweight;
explored = false;
}
Edge() {
explored = false;
}
Weight weight;
Vertex* start;
Vertex* end;
bool explored;
};

class Vertex {
public:
Vertex(Object setelement) {
level = 0;
connectedcomponent = 0;
element = setelement;
back = NULL;
explored = false;
}
Vertex() {
level = 0;
connectedcomponent = 0;
back = NULL;
explored = false;
}
Object element;
vector<Edge*> edge;
vector<Edge*> inedge;
double value;
size_t starttime, finishtime;
size_t level;
size_t connectedcomponent;
float rank;
Vertex* back;
int color;
bool explored;
};

private:

vector<Edge*> edge;

vector<Vertex*> vertex;

size_t counter;

public:
graph();

graph(graph& G);

~graph();

void reset();
void resetBack();
void resetValues();
void resetLevels();
void resetExplored();
void resetConnectedComponents();
vector<Vertex*> incidentVertices(Vertex* v);
vector<Edge*> incidentEdges(Vertex* v);
vector<Edge*> outgoingEdges(Vertex* v);
vector<Vertex*> adjacentVertices(Vertex* v);
size_t indegree(Vertex* v);
size_t outdegree(Vertex* v);
size_t degree(Vertex* v);
Vertex* startVertex(Edge* e);
Vertex* endVertex(Edge* e);

bool isAdjacent(Vertex* v, Vertex* w);

Vertex* insertVertex(Object o);
void insertEdge(Vertex* v, Vertex* w, Weight t);
void insertUndirectedEdge(Vertex* v, Vertex* w, Weight t);
void removeVertex(Vertex* v);
void removeEdge(Edge* e);

size_t numVertices();
size_t numEdges();
vector<Vertex*> vertices();
vector<Edge*> edges();

void print();
void read_file(std::string filename);
};

template <typename Object, typename Weight>
graph<Object, Weight>::graph() {//Default Constructor
Edge();
Vertex();
counter = 0;
}

复制构造函数,生成错误(我知道这是基于迭代器的)

Graph.h:149:22: 错误:‘std::vector::Edge*, std::allocator::Edge*>>::iterator’ 没有名为 ‘start’ 的成员 find(it_edge.start),find(it_edge.end),(*(it_edge))->weight)); ^

Graph.h:149:40: 错误:‘std::vector::Edge*, std::allocator::Edge*>>::iterator’ 没有名为‘end’ 的成员 find(it_edge.start),find(it_edge.end),(*(it_edge))->weight)); ^

template <typename Object, typename Weight>
graph<Object, Weight>::graph(graph<Object, Weight>& G) {//Copy Constructor

typename std::vector<graph<Object, Weight>::Vertex*>::iterator it_vert;

for(it_vert = vertex.begin(); it_vert != vertex.end(); ++it_vert){
vertex.push_back(new Vertex((*(it_vert))->element));
}

typename std::vector<graph<Object, Weight>::Edge*>::iterator it_edge;

for(it_edge = edge.begin(); it_edge != edge.end(); ++it_edge){
edge.push_back(new Edge(
find(it_edge.start),find(it_edge.end),(*(it_edge))->weight));
}

}

最佳答案

find(it_edge.start)

这试图访问对象it_edge 的数据成员startit_edge 是一个迭代器:

typename std::vector<graph<Object, Weight>::Edge*>::iterator it_edge;

您可能想要访问此迭代器(间接)指向的 Edge 对象的数据成员 start

如果您通过 * 将间接寻址应用到迭代器,您将获得迭代器指向的对象(引用):

*it_edge // returns an Edge*

您将需要第二个间接获取此指针指向的 Edge 对象:

**it_edge // returns an Edge object (lvalue)

然后,您可以访问该对象的数据成员start:

(**it_edge).start

通常,-> 递归应用,直到操作数是指针。所以我一开始以为

it_edge->start

也可以。

然而,标准库迭代器需要实现a->m作为(*a).m [input.iterators]/表107。因此,迭代器类从其重载的 operator-> 返回指向元素的指针:

template<class T>
class iterator
{
private:
T* ptr;
public:
T* operator->() { return ptr; }
};

在 OP 中,我们有 T == Edge*,所以 T* == Edge**,这就是原因

it_edge->start

没用。然而,

(*it_edge)->start

工作正常,因为它还应用了两个必要的间接寻址。

关于c++ - 使用迭代器的编译器错误 : "error: ‘...::iterator’ has no member named '...' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23113097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com