Thread: Noobish problem

  1. #16
    Registered User
    Join Date
    Jul 2005
    Posts
    23

    Fixed it, but need advice

    Code:
    #include <iostream>
    
    class myNodeList
    {
     private:
     struct myNode
     {
      char value;
      myNode * next;
     };
     myNode * first;
     myNode * index;
     public:
     myNodeList() : first(0),index(0){}
     ~myNodeList()
     {
      myNode * finger;
      finger=first;
      while(!(finger==0))
      {
       myNode * nullnext;
       nullnext=finger->next;
       delete finger;
       finger = nullnext;
      }
     }
     void addNode(char value)
     {
      myNode * newNode = new myNode();
      newNode->value = value;
      newNode->next = 0;
      if (first==0){first=newNode;}
      else 
      {
       myNode * finger;
       finger=first;
       while(!(finger->next==0)){finger=finger->next;}
       finger->next = newNode;
      }
     }
     void eachNode(void)
     {
      if (index==0){index=first;}
      else {index=index->next;}
     }
     char eachNode_getValue(void)
     {
      return index->value;
     }
     void printNodeList(void)
     {
      myNode * finger;
      finger=first;
      while(!(finger==0)){printf("%c",finger->value);finger=finger->next;};
     }
     int nodeCount(void)
     {
      myNode * finger;
      int count=0;
      finger=first;
      while(!(finger==0)){++count;finger=finger->next;}
      return count;
     }
    };
    int main(void)
    {
     myNodeList nodez;
     nodez.addNode('y');
     nodez.addNode('a');
     nodez.addNode('y');
     nodez.addNode('!');
     nodez.printNodeList();
     printf("\n");
     for (int index=0;index<nodez.nodeCount();++index)
     {
      nodez.eachNode();
      printf("%c.",nodez.eachNode_getValue());
     }
     printf("\n");
     system("PAUSE");
    }
    Works just peachy.

    Thanks for all the advice. The problem came because the FOR loop wasn't evaluating the eachNode along with the index, it was ignoring the part after the comma.

    However, I would like to know if there is a way I can squeeze the call to eachNode() up into the third bracket in FOR. It just looks cleaner to me, don't ask
    Last edited by tol; 07-21-2005 at 03:35 PM.

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Well, you could do it like this. I don't know why, but I had to move the: int index
    outside the for-loop.
    Code:
     int index;
     for (index=0,nodez.eachNode();index<nodez.nodeCount();++index,nodez.eachNode())
    You're right, the problem wasn't calling eachNode() too many times, but that it wasn't initialized before printing the first node.

  3. #18
    Registered User
    Join Date
    Jul 2005
    Posts
    23

    Right but

    This might seem overly picky, but wouldn't putting the int declaration inside the for loop technically be slightly superior because of scope?

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Yes, if you can get it to compile. For some reason I couldn't get it to compile with it declared within the loop. But this would certainly be preferable.

    About an earlier question, you can turn on compiler warnings under Dev-C++ (gcc) with the -Wall option (under Tools -> Compiler Options).

  5. #20
    Registered User
    Join Date
    Jul 2005
    Posts
    23

    Static Pointer problem

    Ok, I got the struct and class version working. All well and good on that, that will be good for what I am going to need it for. Now I am trying my other method just because I am interested, and because I am genuinely interested in messing around with this stuff for the sake of understanding, not just to get what I need right this second and cut out.

    Code:
    class Node {
     private:
     static Node * root; Node * next;
     char content;
     public:
     Node(char content)
     {
      if (root == 0)
      {
       root = this;
       next = NULL;
      }
      else
      {
       Node * finger = root;
       while(!(finger->next==0)){finger=finger->next;}
       finger->next=this;
      }
      this->content = content;
     }
    };
    Now I have a problem. Don't worry too much about the rest of the class, I just included that to make the context clear. What I want to do is make a class that will maintain just a single list of it's type of object. It lacks the flexibility of the way Swoopy showed me, but I am interested in it because I want this to be available when I don't require the separate structs(basically, when I don't need the ability for a program to have more than one list of the given object.)

    If I do it this way
    Code:
    static Node * root; Node * next;
    then it doesn't like root, because it is undefined. [Linker error] undefined reference to `Node::root'

    However, if I attempt to
    Code:
    static Node * root = NULL; Node * next;
    then it says
    4 ...\main.cpp invalid in-class initialization of static data member of non-integral type `Node*'

    This makes me think that it is not liking a class having a static self-pointer. Is there a way around this?

  6. #21
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    When using static member variables you declare it in the class declaration and initialize it outside the class variable:
    Code:
    //A.h
    //////////
    class A
    {
       static A * first;
    };
    
    A::A* first = NULL;
    You're only born perfect.

  7. #22
    Registered User
    Join Date
    Jul 2005
    Posts
    23

    Unhappy More trouble ahead :)

    Thanks! The current state of my code is:

    Code:
    class Node 
    {
     private:
     static Node * root;static Node * index;Node * next;
     char content;
     public:
     Node(char content)
     {
      if (root == NULL)
      {
       root = this;
       next = NULL;
      }
      else
      {
       Node * finger = root;
       while(!(finger->next==NULL)){finger=finger->next;}
       finger->next=this;
      }
      this->content = content;
     }
     static int Count(void)
     {
      Node * finger;int count = 0;
      finger=root;
      while(!(finger->next==NULL)){finger=finger->next;++count;};
     }
    };
    Node::Node * root = NULL;
    Node::Node * index = NULL;
    I am still getting two [Linker error] undefined reference to `Node::root' errors in Dev C++ when I try to construct a node. Crazy, but I still think getting used to C++ will be more than worth it. Any idea why it's not liking this?

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    Node::Node * root = NULL;
    Do you mean
    Code:
    Node::root = NULL;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #24
    Registered User
    Join Date
    Jul 2005
    Posts
    23

    Thanks but

    The compiler seems to like that even less Still, thanks for the help anyhow.

  10. #25
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    Node::Node * Node::root = NULL;

  11. #26
    Registered User
    Join Date
    Jul 2005
    Posts
    23

    Thanks

    Wow, that seems to compile, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM