Help with class members

Hello, i am working on this chunk of code from a textbook and im kinda stuck. The code purpose is to help explain lists in c++. Im supposed to define the member functions find and erase and so far i have this.
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

#include "std_lib_facilities.h"
using namespace std;
 class Link{
 public: 
	string value;
	Link(const string v, Link* p=0, Link* s=0)
		:value(v),prev(p),succ(s){}
		
	Link* insert(Link* n);
	Link* add(Link* n);
	Link* erase();
	Link* find(const string& s);
	const Link* find(const string& s) const;
	
	Link* advance(int n) const;
	Link* next() const{return succ;}
	Link* previous() const {return prev;}

private: 
	Link* prev;
	Link* succ;

};
Link* Link::insert(Link* n)
{

	if(n==0) return this;
	if(this==0) return n;
	n->succ = this;
	if(prev)prev->succ= n;
	n->prev=prev;
	prev=n;
	return n;
	}
Link* Link::find(const string& s)
{
	while(this){
		if(value==s)return this;
		succ;
	}
	return 0;
}
Link* Link::erase(){
	if(this==0) return 0;
	if(succ)succ->prev=prev;
	if(prev)prev->succ=succ;
	return succ;
}
void print_all(Link* p)
{
	cout <<"{";
	while(p){
		cout<<p->value;
		if(p=p->next())cout<<",";
	}
	cout <<"}";
}

int main(){

Link* norse_gods= new Link("Thor"); 					
norse_gods= norse_gods->insert(new Link("Odin"));	
norse_gods= norse_gods->insert(new Link("Zeus"));
norse_gods= norse_gods->insert(new Link("Freia"));

Link* greek_gods= new Link("Hera");
greek_gods=greek_gods->insert(new Link("Athena"));
greek_gods=greek_gods->insert(new Link("Mars"));
greek_gods=greek_gods->insert(new Link("Poseidon"));

Link* p=greek_gods->find("Mars");
if(p)p->value="Ares";

Link* p2=norse_gods->find("Zeus");
if (p2){
	if(p2==norse_gods)norse_gods=p2->next();
	p2->erase();
	greek_gods=greek_gods->insert(p2);
} 

print_all(norse_gods);
cout<<"\n";

print_all(greek_gods);
cout<<"\n";


}




What i noticed was that the find function is not doing its job. I know that if the function wasn't a member function of Link i could write the function like this
1
2
3
4
5
6
7
8

Link* find(link* p. const string& s){
while (p) (
)
if (p->value == s) return p;
p = p->succ;
return 0;
}


giving the value of succ to p. But when using this i know you can't assign it a new value. What im confused is that im stuck as to how to fix it. All im asking is for some guidance so if you could i would definitely appreciate it.
Last edited on
Topic archived. No new replies allowed.