Problems with a vertex

Dec 3, 2013 at 5:37am
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 Dec 3, 2013 at 6:05am
Dec 3, 2013 at 5:58am
closed account (N36fSL3A)
Where is DataType and KeyType being used?
Dec 3, 2013 at 6:05am
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 Dec 3, 2013 at 6:09am
Dec 3, 2013 at 6:06am
Eh... they're not being used. I've removed them from the above post.
Dec 3, 2013 at 6:22am
@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 Dec 3, 2013 at 6:23am
Dec 3, 2013 at 6:52am
Look carefully at lines 37 and 41; you are declaring arrays of vectors there, not vectors with a given size.
Dec 3, 2013 at 7:10am
How would you suggest we change that? so that we are declaring it with a given size and not making it an array?
Dec 3, 2013 at 7:20am
Remove the array syntax on the end and then, in the constructors, initialize the vectors to have the sizes you want.
Dec 3, 2013 at 7:37am
THanks that seemed to work now i'm getting a link error but hopefully i can find the cause of that. :)
Dec 3, 2013 at 8:09pm
What is the error?
Topic archived. No new replies allowed.