I have this program and im trying to get the addVertex method to access the list within the class and return the object "Vertex" whenever i use the brackets. Everything works fine but i cant modify the structure now when i use the brackets. I get an error dealing with the
"->" operator. i tried changing it to a "." but i just get different errors. Ive been staring at this for a while now and any help would be appreciated.


main

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "DirectGraph.cpp"
#include "DirectGraph.h"

using namespace std;

int main()
{
    vector<int> vertexTrack(50,0);
    DirectGraph *directedGraph;
    directedGraph = new DirectGraph();
    ifstream infile("C:\\Users\\Vance\\Documents\\UTC\\Algorithm Analysis\\vertex-text.txt");
    if(infile.is_open())
    {
        while(infile.good())
        {
            string word;
            string strSource;
            string strEnd;
            string strWeight;
            int numSource;
            int edge;
            getline(infile, word);
            if(word.length()==1)
            {
                stringstream convert(word);
                if(!(convert>>edge))
                    edge = 0;
                //edge number equals word[0]

            }
            else
            {
                int pos;
                pos = word.find(" ");
                strSource = word.substr(0, pos);
                word = word.substr(pos+1);
                cout<<word<<endl;
                {
                    stringstream convert(strSource);
                    if(!(convert>>numSource))
                        numSource = 0;
                }
                if(vertexTrack[numSource] == 0)
                {
                    Vertex *vert;
                    vert = new Vertex;
                    vert->dist = 10000;
                    vert->known = false;
                    vert->vertNum = numSource;
                    vert->adj.push_front(word);
                    directedGraph->addVertex(numSource, *vert);
                    vertexTrack[numSource] = numSource;
                }
                else
                {
                    //this is the offending line
                    directedGraph[numSource]->adj.push_front(word);
                }
            }
        }
        infile.close();
    }

    return 0;
}

class file

#include <iostream>
#include "DirectGraph.h"
#include <string>
#include <list>
#include <vector>

DirectGraph::DirectGraph(): vertexList(100)
{
}
void DirectGraph::printShortestPath(int source)
{

}
void DirectGraph::addVertexEdge(int source, std::string end, std::string weight)
{
    std::list<std::string> edgeList;
    std::string strEdge = end + " " + weight;
    edgeList.push_front(strEdge);
    adjList[source] = edgeList;
}
void DirectGraph::addVertex(int index, Vertex vert)
{
    vertexList[index] = vert;
}
//this is the operator overload
Vertex DirectGraph::operator[](int i)
{
    return vertexList[i];
}

header file

#ifndef DIRECTGRAPH_H_INCLUDED
#define DIRECTGRAPH_H_INCLUDED
#include <list>
#include <string>
#include <vector>
#include <string>

struct Vertex
{
    std::list<std::string> adj;
    bool known;
    int dist;
    int vertNum;
    Vertex *path;
};
class DirectGraph
{
    public:
        DirectGraph();
        void printShortestPath(int source);
        void addVertexEdge(int source, std::string end, std::string weight);
        void addVertex(int index, Vertex vert);
        Vertex operator[](int i);
    private:
        std::vector<std::list<std::string> > adjList;
        std::vector<Vertex> vertexList;
};

#endif // DIRECTGRAPH_H_INCLUDED

Your class DirectGraph has an overloaded [] operarator, but your variable directedGraph is a pointer to a DirectGraph object. Your call should look something like:

(*directedGraph)[numSource]->adj.push_front(word);

BTW: I see no good reason why you use a dynamically allocated object, but it's difficult to judge from the piece of code.

Your class DirectGraph has an overloaded [] operarator, but your variable directedGraph is a pointer to a DirectGraph object. Your call should look something like:

(*directedGraph)[numSource]->adj.push_front(word);

BTW: I see no good reason why you use a dynamically allocated object, but it's difficult to judge from the piece of code.

youre completely right. i changed it from dynamically allocated and everything works fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.