-2

I received error messages for the following code. I am new to C++ so I couldn't see what's the problem. Thank you so much.

header file(VGMap.h):

#ifndef VGMAP_H
#define VGMAP_H

namespace Game
{
 struct AdjListNode{
   int data;
   AdjListNode *next;
  };
 struct AdjList{
   AdjListNode *head;
 };
 struct Graph{
   int V;
   AdjList *arr;
 };
 class vgmap{
   public:
   AdjListNode *newAdjListNode(int);
   Graph *createGraph(int);
   void addEdge(Graph*,int,int);
   void printGraph(Graph*);  
 };

}#endif

VGMap.cpp:

#include "VGMap.h"

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::string;

namespace Game
{
  AdjListNode* vgmap::newAdjListNode(int data){
    AdjListNode *nptr=new AdjListNode;
    nptr->data=data;
    nptr->next=NULL;
    return nptr;

  }
  Graph* vgmap::createGraph(int V){
    Graph *graph=new Graph;
    graph->V=V;
    graph->arr=new AdjList[V];
    //initialize with NULL (e.g root=NULL)
    for(int i=0;i<V;i++){
        graph->arr[i].head=NULL;
    }
    return graph;
  }
  void vgmap::addEdge(Game::Graph *graph, int src, int dest){
    AdjListNode *nptr=newAdjListNode(dest);
    nptr->next=graph->arr[src].head;
    graph->arr[src].head=nptr;
    //connect from dest to src (since undirected)
    nptr=newAdjListNode(src);
    nptr->next=graph->arr[dest].head;
    graph->arr[dest].head=nptr;
  }
  void vgmap::printGraph(Game::Graph *graph){
    for(int i=0;i<graph->V;i++){
        AdjListNode *root=graph->arr[i].head;
        if(i==4||(i-4)%5==0){
          cout<<std::endl;
        }
        else if(i%5==0){
          cout<<i<<" ";
        }
        while(root!=NULL){
          if(root->data==i+1){
            cout<<root->data<<" ";
            //i=i+1;
          }
            root=root->next;
        }
    }
  }
}

main.cpp:

#include <iostream>
#include <ctime>

#include "VGMap.h"

using Game::AdjListNode;
using Game::AdjList;
using Game::Graph;
using Game::vgmap;

using std::cout;
using std::cin;
using std::endl;

int main()
{
  int totalVertices=30;
    Graph *graph;
    graph=createGraph(totalVertices);
    //connect edge
    for(int i=0;i<4;i++){
      addEdge(graph,i,i+1);
    }
    for(int i=5;i<9;i++){
      addEdge(graph,i,i+1);
    }
    for(int i=10;i<14;i++){
      addEdge(graph,i,i+1);
    }
    for(int i=15;i<19;i++){
      addEdge(graph,i,i+1);
    }
    for(int i=20;i<24;i++){    
      addEdge(graph,i,i+1);
    }
    for(int i=29;i>25;i--){
      addEdge(graph,i,i-1);
    }
    for(int i=0;i<5;i++){
      addEdge(graph,25-i*5,25-(i+1)*5);
    }
    for(int i=0;i<5;i++){
      addEdge(graph,1+i*5,1+(i+1)*5);
    }
    for(int i=0;i<5;i++){
      addEdge(graph,2+i*5,2+(i+1)*5);
    }
    for(int i=0;i<5;i++){
      addEdge(graph,3+i*5,3+(i+1)*5);
    }
    for(int i=0;i<5;i++){
      addEdge(graph,4+i*5,4+(i+1)*5);
    }
    printGraph(graph);
}

This is the error message I received:

main.cpp:47:11: error: use of undeclared identifier 'createGraph'
    graph=createGraph(totalVertices);
          ^
main.cpp:50:7: error: use of undeclared identifier 'addEdge'
      addEdge(graph,i,i+1);
      ^
main.cpp:53:7: error: use of undeclared identifier 'addEdge'
      addEdge(graph,i,i+1);
      ^
main.cpp:56:7: error: use of undeclared identifier 'addEdge'
      addEdge(graph,i,i+1);
      ^
main.cpp:59:7: error: use of undeclared identifier 'addEdge'
      addEdge(graph,i,i+1);
      ^
main.cpp:62:7: error: use of undeclared identifier 'addEdge'
      addEdge(graph,i,i+1);
      ^
main.cpp:65:7: error: use of undeclared identifier 'addEdge'
      addEdge(graph,i,i-1);
      ^
main.cpp:68:7: error: use of undeclared identifier 'addEdge'
      addEdge(graph,25-i*5,25-(i+1)*5);
      ^
main.cpp:71:7: error: use of undeclared identifier 'addEdge'
      addEdge(graph,1+i*5,1+(i+1)*5);
      ^
main.cpp:74:7: error: use of undeclared identifier 'addEdge'
      addEdge(graph,2+i*5,2+(i+1)*5);
      ^
main.cpp:77:7: error: use of undeclared identifier 'addEdge'
      addEdge(graph,3+i*5,3+(i+1)*5);
      ^
main.cpp:80:7: error: use of undeclared identifier 'addEdge'
      addEdge(graph,4+i*5,4+(i+1)*5);
      ^
main.cpp:82:5: error: use of undeclared identifier 'printGraph'
    printGraph(graph);
    ^
13 errors generated.
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 3
    Why are you calling member functions like they are free functions? – NathanOliver Feb 14 '20 at 16:54
  • 1
    Welcome to Stack Overflow! Please take the [tour] and read [ask]. Concerning your question, please first extract a [mcve] from your code. This example must be contained inline in your question. – Ulrich Eckhardt Feb 14 '20 at 16:54
  • I see a `Game::vgmap::createGraph` function, but no `::createGraph` function. So I agree with your compiler. – Jesper Juhl Feb 14 '20 at 17:01
  • This should be useful: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – eerorika Feb 14 '20 at 17:02

1 Answers1

2

There are no functions defined createGraph or addEdge. There are however methods called vgmap::createGraph and vgmap::addEdge.

Still, you can't call these methods like this because you would need to first create an instance of vgmap, then call the methods on that object.

Since these methods don't act on any internal data, change their definitions to static:

 class vgmap{
   public:
   static AdjListNode *newAdjListNode(int);
   static Graph *createGraph(int);
   static void addEdge(Graph*,int,int);
   static void printGraph(Graph*);  
 };

And then you can call them with the full name without having to create an instance of vgmap:

graph=vgmap::createGraph(totalVertices);
...
vgmap::addEdge(graph,i,i-1);
...
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    I'm afraid this answer still leaves OP clueless as to what static functions are and why using them resolves their problem. And what better alternatives may exist. – Jesper Juhl Feb 14 '20 at 17:49