Thread: help with nodeType function?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    16

    help with nodeType function?

    for some reason when i try to implement this function it says my list is not declared???

    node *find (string address) const;
    // precondition : none
    // postcondition : returns a pointer to node containing address or
    // NULL if not present
    my list(the node)is already declared in the private part of the class header file but when i try to do anything with list it says its not declared? could anyone give me a pointer please? thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Mmm - one line of code - that's gonna be tough to figure out.

    Why not post the whole class or something so we can see the context of the question.

    Also, post the exact error message, not some waffle "isn't declared"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    16

    sorry about that

    ok heres the header file
    Code:
    --------------------------------------------------------------------------------------------------
    #ifndef IPLIST
    #define IPLIST
    
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    struct node
    {
       string IPaddress;
       int count;
       node *link;
    };
    
    class IPlist
    {
       // contains an ordered list of IP addresses and a count of each
       // occurrence
       public:
    
        IPlist();  // default constructor
                   // creates a new empty list
    
        ~IPlist(); // destructor - reallocates all dynamic memory of list
    
        IPlist (const IPlist & other);
                   // copy constructor to create a deep copy of other IPlist
                   // object
    
        bool empty() const; 
                   // precondition : none
                   // postcondition : returns true if list is empty 
                   //                 and otherwise false
    
        void insertInOrder(string address);
                   // precondition : list is ordered and address is not in list
                   // postcondition : list is ordered and contains address
    
        bool isPresent(string address) const;
                   // precondition : none
                   // postcondition : returns true if a node containing address
                   //                is present in list and otherwise false
    
        void updateCount(string address);
                   // precondition : address is in list
                   // postcondition : the count of node with address has been incremented
    
        node *find (string address) const;
                   // precondition : none
                   // postcondition : returns a pointer to node containing address or
                   //                 NULL if not present
    
        void display() const; 
                   // precondition : none
                   // postcondition : the list has been displayed 
                   // one record per line, tab-separated
                   // with heading IPaddress Count
        private:
        
           node *list;
    };
    #endif
    -------------------------------------------------------------------------------------------
    
    this is what i tried to do to implement it:
    
    
      node IPlist::*find (string address) const
                   // precondition : none
                   // postcondition : returns a pointer to node containing address or
                   //                 NULL if not present
                   
     131             {
      132                node *current=list;
       133                   if(current==address)
                          {//checks 1st node
                              //current=list->IPaddress;
       136                       return current;
                          }
                          else 
                          {//increments to the next node
                              return NULL;                         
                          }
                      
                   }
    several errors are then given.......-_-

    131 C:\Documents and Settings\IPlist.cpp non-member function `node IPlist::* find(std::string)' cannot have `const' method qualifier
    132 C:\Documents and Settings\IPlist.cpp `list' undeclared (first use this function)
    133 C:\Documents and Settings\IPlist.cpp no match for 'operator==' in 'current == address'
    136 C:\Documents and Settings\IPlist.cpp cannot convert `node*' to `node IPlist::*' in return

    how to implement this function -_-

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > node IPlist::*find (string address) const
    Isn't the * in the wrong place?

    Like
    node *IPlist::find (string address) const
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    16
    i just added a "IPlist::" to what was declared in the header file? did i do it wrong?

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    16
    lol.....all the errors just disappered except for the

    133 C:\Documents and Settings\IPlist.cpp no match for 'operator==' in 'current == address'

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe current->IPaddress ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    16
    135 C:\Documents and Settings\IPlist.cpp could not convert `((std::string*)current)->std::basic_string<_CharT, _Traits, _Alloc>:perator= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(&address))))' to `bool'

    after changing current to current->IPaddress, getting more and more confused

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well there are two things wrong with void IPlist::insertInOrder
    1. It allocates a new node every time around the loop.
    2. It keeps going even after the node has been inserted.

    Do this in debug mode
    Code:
    ip.insertInOrder("a");
    ip.display();
    ip.insertInOrder("d");
    ip.display();
    ip.insertInOrder("c");
    ip.display();
    ip.insertInOrder("a");
    ip.display();
    Then use the debugger to single step through the code, to see if you can spot where it's all going wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post your latest find function.
    Check what you're comparing and assigning.

    Did you try and assign a string to a node perhaps?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    16
    Code:
     node *IPlist::find (string address) const
                   // precondition : none
                   // postcondition : returns a pointer to node containing address or
                   //                 NULL if not present
                   
                  {
                      node *current=list;
                      while (current!=NULL)
                      {
                          if(current->IPaddress = address)
                          {//checks 1st node
                              return current;
                          }
                          else 
                          {//increments to the next node
                              current=current->link;
                          }
                      }
                      return NULL;
                   }
    this gives error:
    135 C:\Documents and Settings\IPlist.cpp could not convert `((std::string*)current)->std::basic_string<_CharT, _Traits, _Alloc>erator= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(&address))))' to `bool'

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if(current->IPaddress = address)
    Great, you turned what was a comparison (==) into an assignment (=)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    16
    oops lol
    it works fine now thx a lot for you help Salem~!

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    16
    this is my current code
    Code:
    
    #include <iostream>
    #include <cstdlib>
    
    
    using namespace std;
    #include "IPlist.h"
    
    
        IPlist::IPlist()
        // default constructor
        // creates a new empty list
    {
        list=NULL;
    }
    
        IPlist::~IPlist() 
        // destructor - reallocates all dynamic memory of list
    {
        delete [] list;
    }
    
        IPlist::IPlist (const IPlist & other)
                   // copy constructor to create a deep copy of other IPlist
                   // object
    {
       if (other.list==NULL)
       {
          list = NULL;
       }
       else
       {
          // copy the first node
          node *current = other.list;
          list = new node;
          list->IPaddress = current->IPaddress;
          list->link = NULL;
          current = current->link;
          node *pt = list;
          // copy the rest of list
          while (current != NULL)
          {
              pt->link = new node;
              pt = pt->link;
              pt->IPaddress = current->IPaddress;
              pt->link = NULL;
              current = current->link;
          } 
       } 
    }
    
        bool IPlist::empty() const 
        // precondition : none
        // postcondition : returns true if list is empty 
        //                 and otherwise false
    {
             return list==NULL;
    }
                   
    
        void IPlist::insertInOrder(string address)
                   // precondition : list is ordered and address is not in list
                   // postcondition : list is ordered and contains address
                   {
                      while(list!=NULL)
                      {
                        node* current=new node;
                        //temp->IPaddress=address;
                         //node *smaller=list;               
                        
                        if (list->IPaddress < address )
                        {
                            current->IPaddress =address;
                            current->link =list->link;
                            list->link = current;
                            list=current->link;                        
                        }
                        else
                        {
                            list=list->link;
                        }
                      }
                   }
        bool IPlist::isPresent(string address) const
                   // precondition : none
                   // postcondition : returns true if a node containing address
                   //                is present in list and otherwise false
    {
                  node *current=list; 
                  while(current!=NULL)
                  {
                     if (current->IPaddress==address)
                     {
                        return true;
                     }
                     else
                     {
                        current=current->link;
                     }
                  }      
                  return false;
    }
    
        void IPlist::updateCount(string address)
                   // precondition : address is in list
                   // postcondition : the count of node with address has been incremented
    {
                   int count=0;
                   node *current=list;
                   while (current!=NULL)
                   {
                         if (current->IPaddress==address)
                         {
                            current->count=count+1;
                         }               
                         else
                         {   
                            current=current->link;
                         }
                   }
    }
        node *IPlist::find (string address) const
                   // precondition : none
                   // postcondition : returns a pointer to node containing address or
                   //                 NULL if not present
                   
                  {
                      node *current=list;
                      while (current!=NULL)
                      {
                          if(current->IPaddress == address)
                          {//checks 1st node
                              return current;
                          }
                          else 
                          {//increments to the next node
                              current=current->link;
                          }
                      }
                      return NULL;
                   }
    
        void IPlist::display() const
                   // precondition : none
                   // postcondition : the list has been displayed 
                   // one record per line, tab-separated
                   // with heading IPaddress Count
    {
                   node *current=list;
                   while(current!=NULL)
                   {
                       cout<<"IPaddress   Count "<<endl;
                       cout<<current<<"   "<<current->count<<endl;
                       current=current->link;
                   }
    }               
    
    and my main...
    
    
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    #include "IPlist.h"
    void testCopyConstructor(IPlist x);
    int main()
    {
        IPlist ip;
       if (ip.empty())
        {
          cout << "empty works" << endl;
        }
          
         string data[]={"a","b","c","a","b","c","a","d"};
      
        // some strings to insert into IPlist for testing
        // they don't really need to be IP addresses    
        for (int i = 0; i < 8; i++)
        {
           if (!ip.isPresent(data[i]))
              ip.insertInOrder(data[i]);
           else
              ip.updateCount(data[i]);
        }
        ip.display();
        cout << endl;
        node *current = ip.find("d");
        if (current != NULL)
           cout << "found "<< current->IPaddress << endl;
        system("pause");
        return EXIT_SUCCESS;
    }
    
    void testCopyConstructor(IPlist x)
    {
    // copy constructor will be automatically called when pass by value
    // if we have written a correct copy constructor to make a deep copy
    // then changing x will not change the corresponding argument
    // (in this case ip in the main program)
          x.insertInOrder("w");
          x.insertInOrder("x");
    }
    im getting no output when i compile,its just giving me an empty list what error am i making?

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    16
    could anyone help me to fix my problem?
    how to insert data with the insertinorder if the list is empty?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM