Thread: Print pointers

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    Print pointers

    ok so i am trying to print an int that is in a point but it wont let me, this is what i have...
    Code:
    void linkedQueue::addRear(el_t newNum)
    {    
         Node *rear;
         Node *front;
         
         if(count==0)
         {
               rear=new Node;
               rear->elem=newNum;
               front=rear;
         }
         else
         {
               rear->next=new Node;
               rear=rear->next;
               rear->elem=newNum;
         }
    
         count++;
    
    void linkedQueue::displayAll()
    {
         cout<<rear->elem;
    }
    }
    i kno that will not print the whoe list, but im trying to start by just printing one and i cant!!! lol why is this wrong?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the definition of Node? What is the problem that you face? A compile error? Runtime output not what you expected? How does it not work?
    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

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Code:
    struct Node 
    {
           int elem; 
           Node *next; 
    };
    and all im trying to do is print the int at rear->elem, but when i do it says
    'elem' has not been declared

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, I note that you are using rear as a member variable in displayAll(), but you also declared a local Node* variable named rear in addRear(). Are you sure you have a Node* member variable named rear, and that the local variable rear in addRear() is really what you want?
    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
    Oct 2007
    Posts
    9
    im trying to make a linked list that these functions can manipulate. so i have a list and addRear() adds to the rear, displayAll() shows everything in the list, etc.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I guessed as much... the problem should just be that you are using local variables in your member functions but you think you are using member variables.
    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

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    ya i suspected that (complete n00b if you couldnt tell already lol)
    Code:
    #ifndef linkedQueue_H
    #define linkedQueue_H
    
    #include<iostream>
    
    using namespace std;
    
    struct Node 
    {
           int elem;
           Node *next;
    };
    
    class linkedQueue
    {  
          typedef int el_t;
             
          public:
                 linkedQueue();
                 ~linkedQueue();
                 
                 void addRear(el_t newNum);
                 void deleteFront(el_t oldNum);
                 
                 bool isEmpty();
                 int getSize();
                 void displayAll();
                 
          private:
                  int *front,*rear,count;
                  void queueError(char* mesg);
    };
    
    #endif
    is what if have now, and when i put in 'Node *rear;' it gives me all sorts of crap for it. isnt that how i would allow all the functions to use it?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Code:
    int *front,*rear,count;
    You declared them as int* instead of Node*.
    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

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    ooooooooo wow, that would make a difference. thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  2. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 11:24 AM
  3. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM