having problems with the this pointer

Hello guys,

Im stuck on a probably beginners error.
They gave us the unlink function as Item* DList::unlink(Item* ptr) so thats what i put there but then when is use unlink(this) in the destructor it gives the following error:S?
Hopefully someone can help me on this beginners mistake but im already stuck on this one for ages...

Error:
error C2664: 'unlink' : cannot convert parameter 1 from 'Item *const ' to 'const char *

Destructor:
Item::~Item()
{
unlink(this);
// TODO: remove this item from the list it's in
}

function:
Item* DList::unlink(Item* ptr){
if(ptr != 0){//condition ptr is invalid
Item* temp = ptr->_next->_prev;
ptr->_next->_prev = ptr->_prev->_next;
ptr->_prev->_next = temp;
if( ptr == _top_of_ring){
_top_of_ring = _top_of_ring->_next;
}
}
else{
ptr = NULL;
printf("error: pointer does not exist");
}
return(ptr);
};
Hard to tell without your full program, but I can suggest one or two things. First of all, this is a CONSTANT pointer. so your function Item* DList::unlink(Item* ptr) should be
Item* DList::unlink(const Item* ptr).
.
And you can't do ptr = NULL; //constant, remember?
please provide more code
This is all of the code.
The problem im stuck on is the this pointer in the destructor of dlist.cpp


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//////////////////////////////////////////
// Task Double Linked List
// Name ______________ your name _________
//
// file main.cpp
//////////////////////////////////////////
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "dlist.h"
#include <cstdlib>
#include <iostream>



int id;
void main()
{
	DList* my_list;
	// define your my_list here

	char command;
	do
	{
		printf("Type command: ");
		scanf(" %c", &command);
		switch (command)
		{
		case 'a': 
			
			my_list-> append();
			break; // append
		case 'i': 
			my_list-> insert();
			break; // insert				
		case 'd': 
			printf("welke id wil je deleten?");
			scanf(" %i", id);
			my_list->delete_item(id);
			break; // delete
		case 'f': 
			break; // put first
		case 'p': 
			my_list-> print();
			break; // print
		case 's': 
			break; // sort
		case 'x': 
			break; // destroy list
		case 'q': 
			break; // quit
		default :
			printf("Command unknown!\n");
			break;
		}
	}
	while (command != 'q');

	printf("Bye bye!\n");
	// delete your my_list here
}


dlist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Task Double Linked List
// Your name: ______________ your name _________________

#include <stdio.h>
#include "dlist.h"
#include <cstdlib>
#include <iostream>
//////
//
//  Function bodies of members of the Item class
//
//////

Item::Item()
{
	_id = -1;
	_dlist = NULL;// TODO: initialize member values here
}

Item::~Item()
{
	_dlist->unlink(this);
	// TODO: remove this item from the list it's in
}

// Body of the the Item::print member function
// note that the term 'virtual'does not appear here
void Item::print()
{

	printf(" -> Appenden item id %i\n", _id );// of _dlist;






}

// TODO: Implement the other member function bodies below

//////
//
//  End of the Item class
//
//////

//////
//
//  Function bodies of members of the DList class
//
//////

DList::DList()
{
	_top_of_ring = NULL;
	_id_counter = 0;

}

DList::~DList()
{
	// destroy the list
	destroy_list();
}

void DList::destroy_list(void){
	Item* _current_class = _top_of_ring; 
	while(_current_class->_next != _top_of_ring)
	{
		delete _current_class; 
		_current_class = _current_class -> _next;
	}

}

void DList::print(void){
	int i = 0;	//TODO: Implement this function

	Item* _current_class = _top_of_ring; 
	while(_id_counter != 0){
	
	
		printf("-> order %i",  (i+1) );	
		
		_current_class -> print();
		_current_class = _current_class -> _next;
	}
}

Item* DList::create(void){
	 
	return (new Item);

}

Item* DList::append(void){

	Item* temp = _top_of_ring->_prev;
	_top_of_ring->_prev->_next = _top_of_ring->_prev = create();
	_top_of_ring->_prev->_next = _top_of_ring;
	_top_of_ring->_prev->_prev =  temp;

	_top_of_ring->_prev->_id = _id_counter;
	_id_counter++;
	return(_top_of_ring->_prev);
}

Item* DList::insert(void){

	Item* temp = _top_of_ring->_prev;
	_top_of_ring->_prev->_next = _top_of_ring->_prev = create();
	_top_of_ring->_prev->_next = _top_of_ring;
	_top_of_ring->_prev->_prev =  temp;
	_top_of_ring = _top_of_ring->_prev;

	_top_of_ring->_prev->_id = _id_counter;
	_id_counter++;
	return(_top_of_ring->_prev);
}

Item* DList::find(int id){
	int i = 0;
	Item * found = NULL;
	Item * _current_class = _top_of_ring; 
	while(_id_counter != 0){
		if(id == _current_class->_id){
			found = _current_class;
			break;
		}
		_current_class = _current_class -> _next;
	}
	return(found);
};

Item* DList::unlink( Item * ptr){
	if(ptr != 0){//condition ptr is invalid
		Item* temp = ptr->_next->_prev;
		ptr->_next->_prev = ptr->_prev->_next;
		ptr->_prev->_next = temp;
		if( ptr == _top_of_ring){
			_top_of_ring = _top_of_ring->_next;
		}
		return(ptr);
	}
	else{
			return(NULL);
			printf("error: pointer does not exist");
	}
};

int DList::delete_item(int id){
	delete find(id);
	return id;
};

// TODO: Implement the other member function bodies below

//////
//
//  End of the DList class
//
//////


dlist.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef _DLIST_H_INCLUDED
#define _DLIST_H_INCLUDED

// Taks Double Linked List
// Your name: ___________ your name _________________________
//
// The following declaration is necessary to be able
// to declare '_dlist' as a 'DList *' before the Class Dlist
// has actually been defined

class DList;


// The Item class
// This class will serve as a base class for ALL type of items
// that will ever be added to a DList.
// It defines basic functionality

class Item
{
public:
	// constructor
	
	//destructor definition
	// note: function bodies still have to be implemented!
	Item(void);
	~Item(void);

	virtual void print(); // why is this one declared as virtual ?? Think about it!!

	// define additional member function here

protected:
	int _id;	// The id of the item.

private:
	Item* _prev;
	Item* _next;
	DList* _dlist;
	friend class DList;
};

// The DList class
// This class defines basic functionality that one would
// expect from a double linked list.

class DList
{
public:
	DList(void);
	~DList(void);

	// define additional member function here
	void destroy_list();
	void print();
	virtual Item* create();
	virtual Item* append();
	virtual Item* insert();
	virtual Item* find(int id);
	Item* unlink(Item* ptr);
	int delete_item(int id);

private:
	Item* _top_of_ring;
	int _id_counter;
};

#endif



now it complies dlist->unlink(this)
dlist is what i had wrong
Topic archived. No new replies allowed.