Problems with a vertex

Hello all, I am still trying to write a graph program! Unfortunately, I am not getting the error that name is not a member of listVertex[i], even though it is (or at least it should be)... and I have no idea why that is happening.

I've posted the relevant code below. I would be grateful if someone could point out why this isn't working.

in main()
1
2
3
4
5
6
7
8
9
  else{
       for(int i = 0; i<8; i++){//only 8 vertices in my map.txt
          myFile >> Vertext;//string
          cout << "start: " << Vertext<< endl;
          roadMap.addVertex(Vertext);
          cout<<"just added new Vertext" << endl;
       }
       
       roadMap.printGraph();


Code from the .h file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Graph {
   public:
      Graph();
      ~Graph();
      
      void addVertex(string name);
      void printGraph();
      void addEdge();
      bool findVertext();
      void findShortestPath();
      void printPath(); 
      
      class Edge{
        public:
           Edge();
           Edge(int w, string nd){
               weight = w;
               nextDest = nd;
           } 
        
         private:
            string nextDest;
            int weight;
      };

      class Vertext{
         public:
            Vertext();
            Vertext(string n){
                name = n;
            }
            void printVert(){
                 cout<<"PRINT CLASS: "<<name<<endl;
            }

            string name;
            vector<Edge> adjEdges[7];
     };
     
     private:
        vector<Vertext> listVertext [8];
};


From the graph.cpp file


1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Graph<DataType, KeyType>::addVertex(string name){
     Vertext vert(name);
     listVertext.push_back(vert);      
}

void Graph<DataType, KeyType>::printGraph(){
     cout<<"PRINTING GRAPH" <<endl;
     
     for(int i=0; i<8; i++){
         cout<<"CITY NAME: " << listVertext[i].name <<endl;
          //saying there is no name for this and i have no clue why it isn't   
          //working. 
     }        
}


Last edited on
closed account (N36fSL3A)
Where is DataType and KeyType being used?
look at the .h file, at line 30, you use a so called name variable w/c is not declared anywhere in the class
Last edited on
Eh... they're not being used. I've removed them from the above post.
@shadow fiend name is defined in line 36. I thought the fact that it is defined after its first use might have something to do with it, so we moved the constructor's definition from the .h file to the .cpp file. That way, when it defines that constructor, it should already know what name is, but we're still getting the same problem even after we've done that.
Last edited on
Look carefully at lines 37 and 41; you are declaring arrays of vectors there, not vectors with a given size.
How would you suggest we change that? so that we are declaring it with a given size and not making it an array?
Remove the array syntax on the end and then, in the constructors, initialize the vectors to have the sizes you want.
THanks that seemed to work now i'm getting a link error but hopefully i can find the cause of that. :)
What is the error?
Topic archived. No new replies allowed.