473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.
Expand|Select|Wrap|Line Numbers
  1. // code snipped per FAQ and Posting Guidelines
  2. struct listrec* temp; 
  3.  
  4. int main() 
  5. int sum = 0; 
  6. cout << "The list size is " << listsize(*temp); 
  7.  
  8.  
  9.  
  10.  
  11. int listsize(listrec *p) 
  12. struct listrec* temp = p; 
  13. int num=0; 
  14. do{ 
  15. temp = temp -> next; 
  16. num++; 
  17. }while(temp!= NULL); 
  18. return num; 
  19.  
  20.  
  21.  
I get the following error on compilation.

Linker error] undefined reference to `listsize(listrec)'

Please advise.
Mar 31 '07 #1
4 1585
sicarie
4,677 Expert Mod 4TB
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.
Expand|Select|Wrap|Line Numbers
  1. // code snipped per FAQ and Posting Guidelines
  2. struct listrec* temp; 
  3.  
  4. int main() 
  5. int sum = 0; 
  6. cout << "The list size is " << listsize(*temp); 
  7.  
  8.  
  9.  
  10.  
  11. int listsize(listrec *p) 
  12. struct listrec* temp = p; 
  13. int num=0; 
  14. do{ 
  15. temp = temp -> next; 
  16. num++; 
  17. }while(temp!= NULL); 
  18. return num; 
  19.  
  20.  
  21.  
I get the following error on compilation.

Linker error] undefined reference to `listsize(listrec)'

Please advise.
Try moving the declaration for struct listrec* temp inside main
Mar 31 '07 #2
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;
}
Mar 31 '07 #3
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,value3 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'
Mar 31 '07 #4
svlsr2000
181 Expert 100+
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,value3 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&);
Apr 2 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: annie | last post by:
Hi all, I have recently ported my Access 2000 app to SQL Server, keeping the Access client as the front end using linked tables. I am also using triggers on my SQL tables to trap orphan...
7
by: dam_fool_2003 | last post by:
friends, I wanted to learn the various ways of inserting a single list. so: Method 1: #include<stdlib.h> #include<stdio.h> struct node { unsigned int data; struct node *next;
10
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The...
4
by: PRadyut | last post by:
hi, i have written a function in adding a node to the linked list but this only adds "1" to the list as i get in the output I'm using borland c++ compiler The code...
1
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It has to have both a head and a tail pointer, and...
6
by: deanfamily | last post by:
I am re-posting my second problem. I have a double-linked list. I need to know if it is possible to remove just one of an item, instead of all that match the given criteria with the remove()...
9
by: Erik | last post by:
Hi, i have this struct and this linked list /* structure describing a book.*/ typedef struct { char code; char author; char title; int year; int reserved; } Book;
4
by: phe2003 | last post by:
Hi All, I've been testing some extremely simple code about a linked list. What I keep doing is actually writing some parts of a linked list and testing them. Below are my codes:...
12
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.