Can't resolve Error: indirection requires pointer operand ('int' invalid)

59,743

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
Share:
59,743
Jok3r
Author by

Jok3r

Updated on August 03, 2020

Comments

  • Jok3r
    Jok3r over 3 years

    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!

    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()
            {
    
            }
    
    
    };
    
  • Jok3r
    Jok3r about 10 years
    I know that is my problem. How do I fix it though?
  • shf301
    shf301 about 10 years
    @Jok3r get rid of the *. It has no business being there.