gpt4 book ai didi

c++ - 无法解决错误 : indirection requires pointer operand ('int' invalid)

转载 作者:行者123 更新时间:2023-11-30 00:50:56 24 4
gpt4 key购买 nike

我创建了一个顶点指针 vector ,现在我想将它们打印出来。我试图通过取消引用指针来打印它们,但这没有用,我得到了错误:indirection requires pointer operand ('int' invalid)。我以前打印过指针,但我从来没有遇到过这个错误。任何帮助都会很棒!提前致谢!

vector<Vertex*> vertices (numVertices + 1);

for(int i=1;i<=numVertices;i++)
{
file >> numEdges;
cout << "At vertex " << i << " the number of edges is " << numEdges << endl;
vertices[i] = new Vertex();
//Vertex newVertex;

//Using the i counter variable in the outer for loop to identify
//the what vertex what are currently looking at in order to read in the correct adjacent vertex and weight
vertices[i] -> setVertexNum(i);
//newVertex.setVertexNum(i);

for(int j=1;j<=numEdges;j++)
{
file >> adjacentVertex;
cout << "The adjacent vertex is: " << adjacentVertex << endl;

file >> weight;
cout << "The weight is: " << weight << endl;
cout << endl;

vertices[i]->setAdjacentVertex(adjacentVertex, weight);
}
vertices.push_back(vertices[i]);
}
vector<Vertex*> sortedVertices = vertices;
insertionSort(sortedVertices);

for(int i=0;i<vertices.size();i++)
{
cout << "V" << i << ": ";
cout << endl;
for(int j=0;j<*vertices[i] -> getAdjacentVertices().size();j++)
{
cout << "V" << *vertices[i] -> getAdjacentVertices()[j].getAdjVertex() << " " << *vertices[i] -> getAdjacentVertices()[j].getWeight() << endl;
}
}



/////HERE'S MY VERTEX CLASS///
#include "Edge.h"
#include <vector>
#include <climits>
#include <fstream>
using namespace std;
class Vertex
{
private:
int vertexNum; //number of the vertex for identification purposes
int degree;
bool known;
vector<Edge> adjacentVertices; //vector of vertices that are adjacent to the vertex we are currently looking at
int dv; //distance
int pv; //previous vertex
Vertex *vertex;
public:
Vertex()
{
dv = INT_MAX;
known = false;
}

void setKnown(bool Known)
{
known = Known;
}

bool getKnown()
{
return known;
}

void setVertexNum(int VertexNum)
{
vertexNum = VertexNum;
}

void setDegree(int Degree)
{
degree = Degree;
}

vector<Edge> & getAdjacentVertices()
{
return adjacentVertices;
}

int getVertexNum()
{
return vertexNum;
}

int getDegree()
{
return degree;
}

int getDV() const
{
return dv;
}

void setAdjacentVertex(int AdjacentVertex, int Weight)
{
Edge newEdge;
newEdge.setWeight(Weight);
newEdge.setAdjVertex(AdjacentVertex);
adjacentVertices.push_back(newEdge);
}

friend ostream & operator <<(ostream & outstream, Vertex & vertex)
{
outstream << vertex.vertexNum << endl;
outstream << vertex.degree << endl;
outstream << vertex.known << endl;
vector<Edge> E = vertex.getAdjacentVertices();
for(int x=0;x<E.size();x++)
{
outstream << E[x].getAdjVertex() << endl;
outstream << E[x].getWeight() << endl;
}
return outstream;
}

friend bool operator < (const Vertex & v1, const Vertex & v2);

void printVertex()
{

}


};

最佳答案

这是你的问题(以及所有类似的陈述):

*vertices[i] -> getAdjacentVertices()[j].getAdjVertex()

您正在对整数应用指针取消引用(这正是错误消息告诉您的内容)。

您的代码等同于:

int adjVertex = vertices[i] -> getAdjacentVertices()[j].getAdjVertex();
*adjVerder; // Dereferencing an int make no sense

关于c++ - 无法解决错误 : indirection requires pointer operand ('int' invalid),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23327526/

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