How can I access a member from another class?

Hey there, I can't seem to figure out why i can't access the member '*next' from the Node class in the Shuffle function. Any help would be great. Thanks.

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
/*  llist.h
 *  LList
 */

#include <iostream>
#include <string>

using namespace std;

typedef string ElementType;

class LList;
class Node {
protected:
	ElementType data;
	Node *next;

public:
	Node() {next=NULL;};
	Node(ElementType d) {data=d; next=NULL;}
	Node* GetNext() {return next;};
	ElementType GetData() {return data;};
	friend class LList;
	friend ostream& operator<< (ostream &, Node &);
	friend ostream& operator<< (ostream &out, LList &li);

	
};

class LList {
protected:
	Node *first;
	int mySize;
	
public:
	LList() {first = NULL; mySize = 0;}
	~LList();
	void Insert(ElementType, Node *);
	Node* InsertAndReturn(ElementType, Node *);
	void InsertFirst(ElementType);
	void Delete(Node *);
	void DeleteFirst();
	void DeleteAll();
	int FindFirst(ElementType);
	friend ostream& operator<< (ostream &out, LList &li);
	LList& operator= (const LList &li);
	friend LList* Shuffle(LList & , LList &);
	friend class Node;
};


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
/*  llist.cpp
 *  Implementation of member functions for the classes Node and LList.
 */

#include "llist.h"
using namespace std;


ostream& operator<< (ostream &out, Node &n)
{
	out << n.data;
	return out;
}

//
//	Destructor for the List class. Must remember to delete the linked list.
// 

LList::~LList() {
	while (first != NULL) {
		DeleteFirst();
	}
}

//
//	Preconditions: The list has been created
//	Postconditions: A node will be added after the one pointed by p.

void LList::Insert(ElementType e, Node* p) {
	Node *n = new Node(e);
	
	if (p == first) {
		n->next = first;
		first = n;
	}
	else {
		n->next = p->next;
		p->next = n;
	}
}

void LList::InsertFirst(ElementType e) {
	Insert(e,first);
}

Node* LList::InsertAndReturn(ElementType e, Node* p) {
	Node *n = new Node(e);

	if (p == first) {
		n->next = first;
		first = n;
	}
	else {
		n->next = p->next;
		p->next = n;
	}
	return n;
}

void LList::Delete(Node *p) {
	Node *n;
	if (p == first) {
		n = first;
		first = first->next;
	}
	else {
		n = p->next;
		p->next = n->next;
	}
	delete(n);
}

void LList::DeleteFirst() { Delete(first);}

int LList::FindFirst(ElementType e) {
	Node *p = first;
	int i = 0;
	while(p!=NULL) {
		if (p->data == e) return i;
		else { 
			p = p->next;
			i++;
		}
	}
	return -1;
}

void LList::DeleteAll() {
		Node *n;
		int i = 0;
		while (first != NULL) {
			n = first;
			first = first->next;
			cout << "deleting: " << i++ << endl;
			delete(n);
		}
}

ostream& operator<< (ostream &out, LList &li) {
	//	Traverse the list, printing each element
	Node *p = li.first;
	while(p!=NULL) {
		out << *p;
		p = p->next; 
		if (p!=NULL) out << ", ";
	}
	return out;
}

LList& LList::operator= (const LList &li) {
	Node* p;
	Node *r;
	p = li.first;
	r = this->first;
	while(p!=NULL) {
		//cout << "Copying: " << p->data << "  " ;
		r = this->InsertAndReturn(p->data,r);
		p = p->next; 
	}
	return *this;
}


LList* Shuffle(LList &L1, LList &L2){  // Function with errors
	LList L3;
	LList *L3 = new LList;

	Node *p1 = L1.first;
	Node *p2 = L2.first;
	Node *p3 = L3.first;

	L3.first = p1;
	p3 = L3.first;
	p3->next = p2;

	p1 = p1->GetNext();
	p2 = p2->GetNext();
	/*---------------------------------*/

	L3.first = p1;
	p3 = L3.first;
	p3->next = p2;
	p3 = p2;
	p1 = p1->GetNext();
	p2 = p2->GetNext();
	
	
	for(int i = 0; i < L1.mySize-1 ; i++){
		p3->next = p1;
		p3 = p1;
		p3->next = p2;
		p3 = p2;
		p1 = p1->GetNext();
		p2 = p2->GetNext();
	}
	

	return L3;
}


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
#include <iostream>
#include "llist.h"

int main (int argc, char * const argv[]) {

	
	LList *myTestList = new LList;
	
	cout << "will test insertion..." << endl;
	
	myTestList->InsertFirst("abanico");
	myTestList->InsertFirst("berro");
	myTestList->InsertFirst("caballo");
	myTestList->InsertFirst("dado");

	cout << myTestList << endl;
	
	
	cout << "will test delete..." << endl;
	
	myTestList->DeleteFirst();
	
	cout << "List after deleting the first element..." << endl;
	
	cout << myTestList << endl;

	cout << "Finding berro" << endl;
	cout << myTestList->FindFirst("berro") << endl;

	cout << "Finding dado" << endl;
	cout << myTestList->FindFirst("dado") << endl;
	
	//myTestList->DeleteAll();
	
	LList myTestList2;
	myTestList2 = *myTestList;
	cout << "This is the content of TestList2\n" << myTestList2 << endl;
	
	delete(myTestList);
	
	cout << "This is the content of TestList2 after delete of TestList1:\n" << myTestList2 << endl;


/*----------------------------------------------------------------------------------------------------------*/
	cout << "\nAdd an external friend function called 'Shuffle' that " << endl;
	cout << "takes as input two existing lists" << endl;
	cout << "(the ones using linked lists)and creates (and returns) " << endl;
	cout << "a shuffled list of the elements in the two lists." << endl; 
	
	
	LList *newlist1 = new LList;
	LList *newlist2 = new LList;
	LList *newlist3 = new LList;

	newlist1->InsertFirst("10");
	newlist1->InsertFirst("20");
	//newlist1->InsertFirst("30");
	//newlist1->InsertFirst("40");

	newlist2->InsertFirst("8");
	newlist2->InsertFirst("16");
	//newlist2->InsertFirst("24");
	//newlist2->InsertFirst("32");


	LList list1;
	LList list2;
	LList list3;

	list1 = *newlist1;
	cout << "\nThis is the content of List 1:\n" << list1 << endl;

	list2 = *newlist2;
	cout << "\nThis is the content of List 2:\n" << list2 << endl;

	//list3 = *newlist3;
	cout << "\nThis is the content of List 3:\n" << endl;
	newlist3 = Shuffle(list1, list2);
	//cout << list3 << endl;

/*------------------------------------------------------------------------------------------------------------*/

	int dummy;
	cout << "\nCreating a big list so that we can see impact in memory..\n";
	cin >> dummy;
	
	for (int i=0; i<10000; i++) {
		myTestList2.InsertFirst("porqueria");
	}
	
	cin >> dummy;
	delete(&myTestList2);
	
	cin >> dummy;
	
	LList *myTestList3 = new LList;
	cout << "Creating a big list so that we can see impact in memory..\n";
	cin >> dummy;
	
	
	for (int i=0; i<10000; i++) {
		myTestList3->InsertFirst("porqueria");
	}
	
	cin >> dummy;
	delete(myTestList3);
	
	cin >> dummy;
	
	return 0;
}
I'm not willing to compile your code myself. Explain in greater detail what is supposed to happen and what actually happens. You should only post the relevant parts of code. Also, try to debug yourself. That is an incredibly useful skill..
Topic archived. No new replies allowed.