| | |
linked list errors
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2007
Posts: 23
Reputation:
Solved Threads: 0
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.
I get the following error on compilation.
[Linker error] undefined reference to `listsize(listrec)'
Please advise.
c Syntax (Toggle Plain Text)
#include <iostream> using namespace std; struct listrec { int value; struct listrec *next; }; listrec e1,e2,e3; int listsize(listrec); struct listrec* temp; int main() { e1.value = 4; e1.next = &e2; e2.value = 5; e2.next = &e3; e3.value = 3; e3.next = NULL; int sum = 0; cout << "The list size is " << listsize(*temp); system("pause"); return 0; } int listsize(listrec *p) { struct listrec* temp = p; int num=0; do{ temp = temp -> next; num++; }while(temp!= NULL); return num; }
[Linker error] undefined reference to `listsize(listrec)'
Please advise.
Last edited by WaltP; Mar 31st, 2007 at 9:39 pm. Reason: Added CODE tags. Please use them!
You forgot the pointer in the function prototype:
int listsize(listrec*); "Technological progress is like an axe in the hands of a pathological criminal."
•
•
Join Date: Mar 2007
Posts: 23
Reputation:
Solved Threads: 0
Thanx joe. I made the change and a new error. I'm uncertain about the syntax. I need the count of nodes or size of list returned; what should the parameter be in that case?
25 D:\ cannot convert `listrec' to `listrec*' for argument `1' to `int listsize(listrec*)'
c Syntax (Toggle Plain Text)
#include <iostream> using namespace std; struct listrec { int value; struct listrec *next; }; listrec e1,e2,e3; int listsize(listrec*); 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; } int listsize(listrec *p) { struct listrec* temp = p; int num=0; do{ temp = temp -> next; num++; }while(temp!= NULL); return num; )
25 D:\ cannot convert `listrec' to `listrec*' for argument `1' to `int listsize(listrec*)'
Last edited by WaltP; Mar 31st, 2007 at 9:41 pm. Reason: Added CODE tags again.
>int sum = listsize(*temp);
Try removing the *. And what in the world is "temp" doing in your program? You haven't even assigned it to anything when you pass it as an argument to listsize() !
Try removing the *. And what in the world is "temp" doing in your program? You haven't even assigned it to anything when you pass it as an argument to listsize() !
Last edited by John A; Mar 31st, 2007 at 8:44 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
•
•
Join Date: Mar 2007
Posts: 23
Reputation:
Solved Threads: 0
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: new error:
26 D: invalid conversion from `int*' to `int'
D: In function `int listsize(int&)':
39 no match for 'operator!=' in '*p != 0'
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:
c Syntax (Toggle Plain Text)
#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; }
26 D: invalid conversion from `int*' to `int'
D: In function `int listsize(int&)':
39 no match for 'operator!=' in '*p != 0'
Last edited by WaltP; Mar 31st, 2007 at 9:41 pm. Reason: Added CODE tags, again
*groan*
Your code was almost there, but now you wrecked it...
>Write a function called listsize that takes a pointer to start of linked list
OK, think about this. Where is the start of the linked list? Is it some random variable that you just declared, or do you think it's one of the pointers you assigned previously? Think about this carefully.
Your previous attempt was almost there except that you were passing the wrong thing to the function. That's it. This latest "revision" has made things far worse.
Your code was almost there, but now you wrecked it...
>Write a function called listsize that takes a pointer to start of linked list
OK, think about this. Where is the start of the linked list? Is it some random variable that you just declared, or do you think it's one of the pointers you assigned previously? Think about this carefully.
Your previous attempt was almost there except that you were passing the wrong thing to the function. That's it. This latest "revision" has made things far worse.
Last edited by John A; Mar 31st, 2007 at 9:14 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
•
•
Join Date: Mar 2007
Posts: 23
Reputation:
Solved Threads: 0
•
•
•
•
So you need to create a linked list with three nodes and then call a function that follows the links and counts the number of nodes...is that correct?
#include <iostream>
#include <cstddef>
using namespace std;
struct listrec
{
int value;
struct listrec *next;
};
int listsize(listrec*);
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 size = listsize(&e1);
cout << "The list size is " << size;
system("pause");
return 0;
}
int listsize(listrec * head)
{
int num=0;
listrec *p = head;
while(*p!=NULL) // producing error
{
*p = *p -> next;
num++;
};
return num;
}
but I'm down to this error:
40 D: no match for 'operator!=' in '*p != 0'
Last edited by maverick786; Apr 1st, 2007 at 1:20 pm.
![]() |
Similar Threads
- Quicksorting linked list - simple algorithm (C)
- linked list problem!!! (C)
- doubly linked list--- help needed1 (C++)
- Beginner with C++ and goofy run time problem with DBL Linked List (C++)
- Linked List (C++)
Other Threads in the C++ Forum
- Previous Thread: please help the new kid
- Next Thread: C++ .NET random number not getting any output
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






