linked list errors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ironeagle
    New Member
    • Feb 2007
    • 7

    #1

    linked list errors

    I have a program that creates a linked list and is required to count the number of elements in the list. This is what I have thus far.
    Code:
    // code snipped per FAQ and Posting Guidelines
    struct listrec* temp; 
    
    int main() 
    { 
    int sum = 0; 
    cout << "The list size is " << listsize(*temp); 
    
    
    } 
    
    
    int listsize(listrec *p) 
    { 
    struct listrec* temp = p; 
    int num=0; 
    do{ 
    temp = temp -> next; 
    num++; 
    }while(temp!= NULL); 
    return num; 
    
    
    }
    I get the following error on compilation.

    Linker error] undefined reference to `listsize(listr ec)'

    Please advise.
    Last edited by sicarie; Mar 31 '07, 10:44 PM. Reason: Please use [code] and [/code] tags around your code.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by ironeagle
    I have a program that creates a linked list and is required to count the number of elements in the list. This is what I have thus far.
    Code:
    // code snipped per FAQ and Posting Guidelines
    struct listrec* temp; 
    
    int main() 
    { 
    int sum = 0; 
    cout << "The list size is " << listsize(*temp); 
    
    
    } 
    
    
    int listsize(listrec *p) 
    { 
    struct listrec* temp = p; 
    int num=0; 
    do{ 
    temp = temp -> next; 
    num++; 
    }while(temp!= NULL); 
    return num; 
    
    
    }
    I get the following error on compilation.

    Linker error] undefined reference to `listsize(listr ec)'

    Please advise.
    Try moving the declaration for struct listrec* temp inside main

    Comment

    • ironeagle
      New Member
      • Feb 2007
      • 7

      #3
      Originally posted by sicarie
      Try moving the declaration for struct listrec* temp inside main
      I did have it in there, but I moved it to the top now and I get the following error:
      35 D: cannot convert `listrec' to `listrec*' in initialization

      int main()
      { struct listrec* temp;

      e1.value = 4;
      e1.next = &e2;
      e2.value = 5;
      e2.next = &e3;
      e3.value = 3;
      e3.next = NULL;

      int sum = listsize(*temp) ;

      cout << "The list size is " << sum;

      system("pause") ;
      return 0;
      }

      Comment

      • ironeagle
        New Member
        • Feb 2007
        • 7

        #4
        I think I have been going at this all wrong. Let me state the rquirements:
        Write a function called listsize that takes a pointer to start of linked list and returns number of elements.
        Write a main to create linked list of 4,5,3 as value1,value2,v alue3 and the call the function to calculate the size and print it.
        Here is what I have:
        #include <iostream>
        using namespace std;
        struct listrec
        {
        int value;
        struct listrec *next;
        };


        int listsize(int);
        struct listrec *p;
        int main()
        {
        struct listrec e1,e2,e3;
        e1.value = 4;
        e1.next = &e2;
        e2.value = 5;
        e2.next = &e3;
        e3.value = 3;
        e3.next = NULL;
        int x =0;

        int sum = listsize(&x);

        cout << "The list size is " << sum;

        system("pause") ;
        return 0;
        }
        int listsize(int &x)
        {

        int num=0;

        while(*p!= NULL);
        {
        *p = *p -> next;
        num++;

        }
        return num;
        }

        new error:
        26 D: invalid conversion from `int*' to `int'
        D: In function `int listsize(int&)' :
        39 no match for 'operator!=' in '*p != 0'

        Comment

        • svlsr2000
          Recognized Expert New Member
          • Feb 2007
          • 181

          #5
          Originally posted by ironeagle
          I think I have been going at this all wrong. Let me state the rquirements:
          Write a function called listsize that takes a pointer to start of linked list and returns number of elements.
          Write a main to create linked list of 4,5,3 as value1,value2,v alue3 and the call the function to calculate the size and print it.
          Here is what I have:
          #include <iostream>
          using namespace std;
          struct listrec
          {
          int value;
          struct listrec *next;
          };


          int listsize(int);
          struct listrec *p;
          int main()
          {
          struct listrec e1,e2,e3;
          e1.value = 4;
          e1.next = &e2;
          e2.value = 5;
          e2.next = &e3;
          e3.value = 3;
          e3.next = NULL;
          int x =0;

          int sum = listsize(&x);

          cout << "The list size is " << sum;

          system("pause") ;
          return 0;
          }
          int listsize(int &x)
          {

          int num=0;

          while(*p!= NULL);
          {
          *p = *p -> next;
          num++;

          }
          return num;
          }

          new error:
          26 D: invalid conversion from `int*' to `int'
          D: In function `int listsize(int&)' :
          39 no match for 'operator!=' in '*p != 0'

          declaration int listsize(int); is not same as int listsize(int &);, you have listsize as int listsize(int); and defined it as int listsize(int&);

          Comment

          Working...