C++ 无法解决错误:间接需要指针操作数('int'无效)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23327526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't resolve Error: indirection requires pointer operand ('int' invalid)
提问by Jok3r
I created a vector of vertex pointers and now I want to print them out. I tried to print them out by dereferencing the pointers, but that didn't work and I got Error: indirection requires pointer operand ('int' invalid)
. I've printed out pointers before, but I have never run into this error. Any help would be great! Thanks in advance!
我创建了一个顶点指针向量,现在我想将它们打印出来。我试图通过取消引用指针来打印它们,但这没有用,我得到了 Error: 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()
{
}
};
回答by shf301
This is your problem (and all the statements like it):
这是你的问题(以及所有类似的陈述):
*vertices[i] -> getAdjacentVertices()[j].getAdjVertex()
You are applying a pointer dereference to an integer (which is exactly what the error message is telling you).
您正在对整数应用指针取消引用(这正是错误消息告诉您的内容)。
Your code is equatly equivalent to:
您的代码等同于:
int adjVertex = vertices[i] -> getAdjacentVertices()[j].getAdjVertex();
*adjVerder; // Dereferencing an int make no sense