Thread: Rusty with c++ need some help with errors involving pointers and stuff

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    25

    Rusty with c++ need some help with errors involving pointers and stuff

    Bit rusty with C++ and not sure how to fix these errors i'm getting. Any help is much appreciated!!!

    Here's the errors:

    DFS.cpp: In function 'void DFS(Node*)':
    DFS.cpp:64: error: cannot convert 'Node' to 'Node*' for argument '1' to 'void DFS_Visit(Node*)'
    DFS.cpp: In function 'Node* makeGraph()':
    DFS.cpp:130: error: no match for 'operator*' in '**(((Node*)(((long unsigned int)i) * 16ul)) + graph)'
    DFS.cpp: In function 'Node* makeSample()':
    DFS.cpp:150: error: no match for 'operator=' in '*(((Node*)(((long unsigned int)i) * 16ul)) + graph) = 0l'
    DFS.cpp:15: note: candidates are: Node& Node:: operator=(const Node&)
    DFS.cpp:158: error: name lookup of 'i' changed for new ISO 'for' scoping
    DFS.cpp:148: error: using obsolete binding at 'i'



    And here's the code:

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    
    using namespace std;
    
    
    int operationsCounter=0;
    int ntime=0, verts, edges;
    string* color = new string[1];
    int* pi = new int[1];
    int* d = new int[1];
    int* f = new int[1];
    
    struct Node{
      int data;
      Node* next;
    };
    void InsertAfter(Node *head, int value){
      if(head==NULL){
        Node* temp=NULL;
        temp = new Node;
        temp->data = value;
        temp->next = NULL;
        head = temp;
      }else{
        Node *temp = new Node;
        temp->data = value;
        temp->next = (head)->next;
        (head)->next = temp;
      }
    }
    
    
    void DFS_Visit(Node* u)
    {
      color[u->data]="gray";
      d[u->data]=(++ntime);
      int v=u->data;
      while((u=u->next)!=NULL)
      {
        if (color[u->data]=="white")
        {
          pi[u->data]=v;
          DFS_Visit(u);
        }
      }
      color[v]="black";
      f[v]=(++ntime);
    }
    
    void DFS(Node* G)
    {
      for (int u=0; u<verts; u++)
      {
        color[u]="white";
        pi[u]=-1;
      }
      ntime=0;
      for (int u=0; u<verts; u++)
      {
        if (color[u]=="white")
        {
          DFS_Visit(G[u]);
        }
      }
    }
    
    void printResults()
    {
      cout << "       d    f    pi " << endl;
      for (int i=0; i<verts; i++)
      {
        if(i<10)
          cout << "    " << i;
        else if(i<100)
          cout << "   " << i;
        else if(i<1000)
          cout << "  " << i;
        else
          cout << " " << i;
        if(d[i]<10)
          cout << "    " << d[i];
        else if(d[i]<100)
          cout << "   " << d[i];
        else if(d[i]<1000)
          cout << "  " << d[i];
        else
          cout << " " << d[i];
        if(f[i]<10)
          cout << "    " << f[i];
        else if(f[i]<100)
          cout << "   " << f[i];
        else if(f[i]<1000)
          cout << "  " << f[i];
        else
          cout << " " << f[i];
        if(pi[i]<10)
          cout << "    " << pi[i] << endl;
        else if(pi[i]<100)
          cout << "   " << pi[i] << endl;
        else if(pi[i]<1000)
          cout << "  " << pi[i] << endl;
        else
          cout << " " << pi[i] << endl;
        
      }
      cout << endl << endl; 
      cout << "Number of operation: " << operationsCounter << endl;
    }
    
    bool alreadyInList(Node* llist, int data)
    {
      if(llist==NULL)
        return false;
      else if(data==llist->data)
        return true;
      else
        return alreadyInList(llist->next, data);
    }
    
    Node* makeGraph()
    {
      int u, v;
      u=0;
      v=0;
      Node* graph = new Node[5];
      for(int i=0; i<5; i++)
      {
        &graph[i]=NULL;
        InsertAfter(&graph[i], i);
      }
      for(int i=0; i<edges; i++)
      {
        while(alreadyInList(&graph[u], v))
        {
          u = rand() % verts;
          v = rand() % verts;
        }
        InsertAfter(&graph[u], v);
      }
      return graph;
    }
    
    Node* makeSample()
    {
      Node* graph = new Node[5];
      for(int i=0; i<verts; i++)
      {
        graph[i]=NULL;
        InsertAfter(&graph[i], i);
      }
      InsertAfter(&graph[0], 1);
      InsertAfter(&graph[1], 2);
      InsertAfter(&graph[1], 3);
      InsertAfter(&graph[2], 3);
      InsertAfter(&graph[2], 4);
      InsertAfter(&graph[4], i);
      return graph;
    }
    
    int main()
    {
      int option;
      while (option!=3)
      {
        cout << "Please select an option: " << endl;
        cout << "1) Run DFS on the sample graph" << endl;
        cout << "2) Run DFS on a customized random graph" << endl;
        cout << "3) Exit" << endl;
        cin >> option;
        if(option==1)
        {
          verts=5;
          edges=6;
    	  color = new string[verts];
          pi = new int[verts];
          d = new int[verts];
          f = new int[verts];
          operationsCounter=0;
          DFS(makeSample());
          printResults();
        }
        if(option==2)
        {
          cout << "How many vertices would you like the graph to have?" << endl;
          cin >> verts;
          cout << "How many edges would you like the graph to have?" << endl;
          cin >> edges;
    	  color = new string[verts];
          pi = new int[verts];
          d = new int[verts];
          f = new int[verts];
          operationsCounter=0;
          DFS(makeGraph());
          printResults();
        }
      }
      cout << "Thank you.  Goodbye!";
      return 0;
    }

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    For
    DFS.cpp: In function 'void DFS(Node*)':
    DFS.cpp:64: error: cannot convert 'Node' to 'Node*' for argument '1' to 'void DFS_Visit(Node*)'
    change
    Code:
    DFS_Visit(G[u]);
    to:
    Code:
    DFS_Visit( &G[u] );
    For
    DFS.cpp:150: error: no match for 'operator=' in '*(((Node*)(((long unsigned int)i) * 16ul)) + graph) = 0l'
    DFS.cpp:15: note: candidates are: Node& Node:: operator=(const Node&)
    try changing
    Code:
    graph[i]=NULL;
    to:
    Code:
    &graph[i]=NULL;
    For
    DFS.cpp:158: error: name lookup of 'i' changed for new ISO 'for' scoping
    DFS.cpp:148: error: using obsolete binding at 'i'
    Move the int i = 0; out before the for loop in:
    Code:
    for(int i=0; i<verts; i++)

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    Ah thanks! But

    Code:
    &graph[i]=NULL;
    still gives the error:

    DFS.cpp: In function 'Node* makeGraph()':
    DFS.cpp:130: error: invalid lvalue in assignment

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I do not think you can assign NULL to the address of an object. What you can do is assign NULL to a pointer. But graph is a pointer, not an array of pointers. Consequently, I think you want to make graph a dynamic array of pointers instead. Either that, or you are trying to zero each node, but that would have already been done for you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    Yes, I want it to be an array of pointers (Nodes)..... How do I do that then?

  6. #6
    Banned
    Join Date
    Nov 2007
    Posts
    678
    How did the OP post this syntax highlighted C++ code?
    Is it automatically enabled now, i think i need to post some code to test it!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, I want it to be an array of pointers (Nodes)..... How do I do that then?
    You could write:
    Code:
    Node** graph = new Node*[5];
    However, there are more problems:
    • Why do you use global variables in your code?
    • Why do you give your variables such undescriptive names as pi, d and f?
    • Where is your delete[] to match your new[]?
    • Where is your delete to match your new?
    • In main, why do you use option before it is initialised?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    u->data is causing a seg fault...... why?

    in particular the first time i think it is used is in (where i know it is causing the seg fault)

    color[u->data]="gray";

    in DFS_Visit

    any thoughts?

  10. #10
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Code:
    template <typename Unknown>
    class Shahrukh : public FilmStar {
    
        int i;
        float f;
    
    public:
        void dance();
        void act();
        long double earn();
    };

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you fixed the problems I listed? If so, post your updated code. If not, fix them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    Quote Originally Posted by laserlight View Post
    You could write:
    • Why do you use global variables in your code?
    • Why do you give your variables such undescriptive names as pi, d and f?
    • Where is your delete[] to match your new[]?
    • Where is your delete to match your new?
    • In main, why do you use option before it is initialised?
    -multiple functions modifying them, so only way I know
    -hw assignment and thats what they said to use, lol, its for an algorithms class so they know what they are supposed to be (pi=parent, d=discovered time, f=finished time)
    -don't have one.... don't really need it
    -just initialized it to 0 to begin with

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    i have extra couts in there to find the seg fault so you can just ignore them....

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    
    using namespace std;
    
    
    int operationsCounter=0;
    int ntime=0, verts, edges;
    string* color = new string[1];
    int* pi = new int[1];
    int* d = new int[1];
    int* f = new int[1];
    
    struct Node{
      int data;
      Node* next;
    };
    void InsertAfter(Node *head, int value){
      if(head==NULL){
        Node* temp=NULL;
        temp = new Node;
        temp->data = value;
        temp->next = NULL;
        head = temp;
      }else{
        Node *temp = new Node;
        temp->data = value;
        temp->next = (head)->next;
        (head)->next = temp;
      }
    }
    
    
    void DFS_Visit(Node* u)
    {
    	cout << "---in DFS_Visit u=" << endl;
    	cout << "---" << &u->data << endl;
      color[u->data]="gray";
      cout << "---color" << endl;
      d[u->data]=(++ntime);
      cout << "---d" << endl;
      int v=u->data;
      cout << "---DFS_Visit before while" << endl;
      while((u=u->next)!=NULL)
      {
    	  cout << "---DFS_Visit in while u=" << u->data << endl;
        if (color[u->data]=="white")
        {
          pi[u->data]=v;
          DFS_Visit(u);
        }
      }
      cout << "---DFS_Visit after while" << endl;
      color[v]="black";
      f[v]=(++ntime);
    }
    
    void DFS(Node** G)
    {
      for (int u=0; u<verts; u++)
      {
        color[u]="white";
        pi[u]=-1;
      }
      ntime=0;
      cout << "---DFS 1" << endl;
      for (int u=0; u<verts; u++)
      {
        if (color[u]=="white")
        {
          DFS_Visit(G[u]);
        }
      }
      cout << "---DFS 2 done" << endl;
    }
    
    void printResults()
    {
      cout << "       d    f    pi " << endl;
      for (int i=0; i<verts; i++)
      {
        if(i<10)
          cout << "    " << i;
        else if(i<100)
          cout << "   " << i;
        else if(i<1000)
          cout << "  " << i;
        else
          cout << " " << i;
        if(d[i]<10)
          cout << "    " << d[i];
        else if(d[i]<100)
          cout << "   " << d[i];
        else if(d[i]<1000)
          cout << "  " << d[i];
        else
          cout << " " << d[i];
        if(f[i]<10)
          cout << "    " << f[i];
        else if(f[i]<100)
          cout << "   " << f[i];
        else if(f[i]<1000)
          cout << "  " << f[i];
        else
          cout << " " << f[i];
        if(pi[i]<10)
          cout << "    " << pi[i] << endl;
        else if(pi[i]<100)
          cout << "   " << pi[i] << endl;
        else if(pi[i]<1000)
          cout << "  " << pi[i] << endl;
        else
          cout << " " << pi[i] << endl;
        
      }
      cout << endl << endl; 
      cout << "Number of operation: " << operationsCounter << endl;
    }
    
    bool alreadyInList(Node* llist, int data)
    {
    	cout << "---already in list" << endl;
      if(llist==NULL)
        return false;
      else if(data==llist->data)
        return true;
      else
        return alreadyInList(llist->next, data);
    }
    
    Node** makeGraph()
    {
      int u, v;
      u=0;
      v=0;
      Node** graph = new Node*[5];
      for(int i=0; i<5; i++)
      {
        graph[i]=NULL;
        InsertAfter(graph[i], i);
      }
      for(int i=0; i<edges; i++)
      {
        while(alreadyInList(graph[u], v))
        {
          u = rand() &#37; verts;
          v = rand() % verts;
        }
        InsertAfter(graph[u], v);
      }
      return graph;
    }
    
    Node** makeSample()
    {
      Node** graph = new Node*[5];
      int i=0;
      while(i<verts)
      {
        graph[i]=NULL;
        InsertAfter(graph[i], i);
    	i++;
      }
      InsertAfter(graph[0], 1);
      InsertAfter(graph[1], 2);
      InsertAfter(graph[1], 3);
      InsertAfter(graph[2], 3);
      InsertAfter(graph[2], 4);
      InsertAfter(graph[4], i);
      cout << "---Sample graph made ..........es." << endl;
      return graph;
    }
    
    int main()
    {
      int option=0;
      while (option!=3)
      {
        cout << "Please select an option: " << endl;
        cout << "1) Run DFS on the sample graph" << endl;
        cout << "2) Run DFS on a customized random graph" << endl;
        cout << "3) Exit" << endl;
        cin >> option;
        if(option==1)
        {
          verts=5;
          edges=6;
    	  color = new string[verts];
          pi = new int[verts];
          d = new int[verts];
          f = new int[verts];
          operationsCounter=0;
          DFS(makeSample());
          printResults();
        }
        if(option==2)
        {
          cout << "How many vertices would you like the graph to have?" << endl;
          cin >> verts;
          cout << "How many edges would you like the graph to have?" << endl;
          cin >> edges;
    	  color = new string[verts];
          pi = new int[verts];
          d = new int[verts];
          f = new int[verts];
          operationsCounter=0;
          DFS(makeGraph());
          printResults();
        }
      }
      cout << "Thank you.  Goodbye!";
      return 0;
    }

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    -multiple functions modifying them, so only way I know
    You obviously know another way Pass them to the functions as arguments. There is yet another way, which I think manav is hinting at: they may be better off as member variables of a class.

    -hw assignment and thats what they said to use, lol, its for an algorithms class so they know what they are supposed to be (pi=parent, d=discovered time, f=finished time)
    Your lecturer is entitled to use abbreviations in class to fit everything in the lecture slides. You have no such privilege since your code can span much more than lecture slides reasonably can, thus you should give appropriate names to your variables. Names like parent, discovered_time and finished_time certainly are not too long, and yet are descriptive enough.

    -don't have one.... don't really need it
    You do need to match your new[] with delete[] and your new with delete unless you want memory leaks.

    -just initialized it to 0 to begin with
    Your code says otherwise.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    - thats just ugly and more work than required and since its now almost 2 a.m. and i'm running on 2 hours of sleep definately feeling lazy :-P
    - actually the assignment description says to call them that.... sure i doubt i'd lose points if i did use something else, but i've also got this algorithm memorized with pi, d, and f so works for me
    -doesn't matter for this class...
    -int option=0;
    while (option!=3)
    nope looks initialized before use to me :-P

    but as for the seg fault.... what wrong with color[u->data]="gray";

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning pointers
    By spudval in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2007, 02:12 PM
  2. References vs Pointer vs copying and storing stuff in classes
    By Monkeymagic in forum C++ Programming
    Replies: 20
    Last Post: 09-01-2006, 09:40 AM
  3. kinda wierd stuff with pointers...
    By Noobwaker in forum C Programming
    Replies: 4
    Last Post: 04-17-2006, 02:21 PM
  4. delete and delete[]
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2003, 04:40 AM