Removing a element from a template Linked List

Hi All! I have been stumbled upon this one for quite awhile. I have been trying to implement a way to remove a post from a list of posts implemented with a template doubly linked list. I have thought that the best way to do this might by operator overloading, but I have digressed. I have just thought of using a isEqual that checks equality, but when trying to implement i'm getting weird errors.

This is within my class wall, which is a linked list of wall posts, getPostInfo is within the class WallPost.

1
2
3
4
5
6
7
bool isEqual(WallPost const & a, WallPost const & b)
{
	if(a.getPostInfo() == b.getPostInfo())
		return true;
	else
		return false;
}


I have several instances of the error "void illegal with all types" on line 3.
It also is complaining about a not being a arithmetic, unscoped enum, or pointer type. I am assuming that it is because my getPostInfo function is a void.

Thank you for the help.
What does getPostInfo() return? Please show some more code.
My apologies, it asks the user for the information regarding the post, then sets it equal to private values in the Wallpost class. This is used to print out the contents of the post in a list or individually.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
void WallPost::getPostInfo()  //Gets information about post, calls setPostInfo to get information
{
	string n,m;
	cout << "Who is the author of the post?" << endl; 
	std::getline(std::cin,n);
	cin.clear();
	cout << "Enter post: ";
	std::getline(std::cin,m);
	setPostInfo(n,m);
}

void WallPost::setPostInfo(string n, string m)
{
	PostAuthor = n;	
	Post = m;
}
Your getPostInfo() does not return anything.

if(a.getPostInfo() == b.getPostInfo())

This statement does not have any meaning. What do you mean by this condition?
I clearly wasn't thinking this through. It wouldn't have any value if nothing is returned. I switched it to a string and that seemed to work. Now I will have to go about putting it in my remove function so that it may delete the correct posts. Thanks!
You're welcome!
You should include some sort of id piece to your data structure. I typically add an unsigned long long to a data structure I know I will have to find. This "id" is then assigned as a unique id to that individual structure (link data structure). I can then scan the list, later, with 1 structure that tells me the id, and with no reference, I can find exactly the right one it refers to while allowing flexibility on the useer-end.

Another way is to scan through from the beginning until you find the "x"th link.

However, I haven't been able to get my linked list to work.... data conversions/whatnot aren't going like I thought, and my compiler doesn't like somthing about my pointers... lol

In any case, good luck with your work.
Last edited on
IWishIKnew, I have tried something to the method you have prescribed prior to you describing it here. I have also came across frustrating pointer problems.


In the meantime, I am getting a unresolved external symbol error after implementing the isEqual function in the remove function in my doubly linked list.

Which is implemented as such
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void double_linked<T>::deleteNode(T n)
{
	Node<T> *p;
	p = head;

	while (p != NULL)
	{
		if (isEqual(p->data,n))
		{
			Node<T> *q = p->next;
			removePointer(p);
			p = q;
		}
	}
}



Error 1 error LNK2019: unresolved external symbol "public: bool __thiscall double_linked<class WallPost>::isEqual(class WallPost const &,class WallPost const &)" (?isEqual@?$double_linked@VWallPost@@@@QAE_NABVWallPost@@0@Z) referenced in function "public: void __thiscall double_linked<class WallPost>::deleteNode(class WallPost)" (?deleteNode@?$double_linked@VWallPost@@@@QAEXVWallPost@@@Z) \HW5\HW5\Wall.obj


I think my error results from the way I include my .h and .cpp files, but I am uncertain.
Last edited on

Just use the == operator and add it to any onjects you wantto use t with.
It looks like the object file that contains bool isEqual(WallPost const & a, WallPost const & b) is not being linked into your executable.

The compiling seems fine, so it's probably not a problem with how you include header files.

How are you building your project? How many .cpp files do you have, and how do you compile them all? How do you link the object files?
Last edited on
This is within my class wall, which is a linked list of wall posts, getPostInfo is within the class WallPost.

1
2
3
4
5
6
7
bool isEqual(WallPost const & a, WallPost const & b)
{
	if(a.getPostInfo() == b.getPostInfo())
		return true;
	else
		return false;
}



By "within" class wall, do you mean it is a member of a class? On line 8 of the code below, you call a freestanding isEqual and not one that is a member method of any class (unless it is a member of double_linked<T>).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void double_linked<T>::deleteNode(T n)
{
	Node<T> *p;
	p = head;

	while (p != NULL)
	{
		if (isEqual(p->data,n))
		{
			Node<T> *q = p->next;
			removePointer(p);
			p = q;
		}
	}
}
Last edited on
It turned out that I had bool isEqual defined in my double_linked header and my Wall header. I removed the double_linked header and it compiled. That doesn't necessarily mean it works though. Thanks!
um.... if you're writing a function to returm the result of a boolean operator, you should really just use the operator....

aka:

instead of bool iseaqual(), just use (a == b). If you're so compelled to write a function for it, though I suggest returning the operation directly...

1
2
3
4
5
templat<class tp1, class tp2>
bool isequal(const tp1& v1, const tp2& v2)
{
    return (v1 == v2);
}


of course.... you could still just use (a == b)... (it's a matter of efficiency)
Hi all, this is a couple days old, but I did get this working. However, I am having trouble getting this to work with a list of users. I keep getting some form of the error unhandled exception,

Unhandled exception at 0x011259E6 in HW3.exe: 0xC0000005: Access violation writing location 0xFDFDFDFD.

Which seems to be an error with my pointers, but after several hours of debugging I am stumped.

Remove post function
1
2
3
4
5
6
7
8
9
10
11
void UserList::removeUserfromList()
{
	string id;
	User *d = new User();
	
	cout << "Enter the ID of the user you would like to remove: ";
	std::getline(std::cin, id);
	d->setUserID(id);
	
	LinkedUserList->deleteNode(*d);
}



Doubly Linked List Remove function, checks if user's input is equal to the id record in the userlist. If it is will pass on to removepointer and delete.

UserList constructor/deconstructor

1
2
3
4
5
6
7
8
9
UserList::UserList(void)
{
	LinkedUserList = new double_linked<User>;
}

UserList::~UserList(void)
{
	delete LinkedUserList;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <class T>
void double_linked<T>::deleteNode(T n)
{
	Node<T> *p;
	p = head;
	
	while (p != NULL)
	{
		if (isEqual(p->data,n))
		{
			Node<T> *q = p->next;
			removePointer(p);
			p = q;
		}
		else p = p->next;
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template <class T>
double_linked<T>::double_linked(void)
{
	head=tail= NULL;
	size = 0;
}



template <class T>
double_linked<T>::~double_linked(void)
{
	Node<T> * tmp = NULL;
	while (head)
	{
		tmp = head;
		head = head->next;
		delete tmp;
	}
	head = tail = NULL;

}


I have a good feeling it is something minuscule, thanks again.
Use a debugger to find exactly what line it's crashing on, and exactly what the state of the various pointers are. That should tell you which pointer has gotten messed up. Then work backwards from there, to figure out why that pointer has gotten messed up.
Thanks for the quick response, i'll give it another go.
It seems the errors unhandle errors I am receiving are inconsistent, but particularly my IDE (VS2013) opens up isodwf.cpp and puts a break at
line 3. It looks like it would it appear that there is something wrong with the pointer on the left hand assignment, possibly out of range or not initialized?

1
2
3
4
static void __CLRCALL_OR_CDECL assign(_Elem& _Left, const _Elem& _Right) _NOEXCEPT
		{	// assign an element
		_Left = _Right;
		}


Still using dbg to see if I can find the source.
Topic archived. No new replies allowed.