Error while returning an object pointer?

The code should traverse through a doubly linked list and return a pointer that points to an object node or returns a NULL pointer otherwise. That node that is found should have the same id that is being passed to the function.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
node * find(int id)
	{
		node * finder = head;

		while(finder != NULL)
		{
			if(finder -> data.id == id)
			{
				return finder;
			}

			else
			{
				finder = finder -> next;
			}
		}

		return finder;
	}


I receive these errors:
error C2143: syntax error : missing ';' before '*'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2065: 'finder' : undeclared identifier
error C2065: 'head' : undeclared identifier
error C2065: 'finder' : undeclared identifier

I'm stuck and need some help. I've been searching online to find out what is wrong but I haven't had any luck in quite some time.

My node class is within the doubly linked list class called studentList.

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
class studentList
{
private:

	class node
	{
	public:

		student data;
		node * next;
		node * prev;
	};

	node * head;

public:

	studentList();
	//~studentList();
	void add(student s);
	void removeStudent(int id);
	//void sort(string field) const;
	void display() const;
	node * find(int id);

};
Last edited on
hello, I think that you are on the right track.
would it be possible to see your node class?
My node and linkedlist class was added.
It might have something to do with the node class being nested within the studentList class.
Oh wait, I believe it's because I didn't apply the scoping operator to the method.

I should have put node * studentList::find(int id) instead of node * find(int id). Hold on.
I think that the issue is that the head object can't be accessed statically. Have you tried doing something like
node *finder = this -> head
?
Ok so I applied the scoping operator. Here is the new code and errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
node * studentList::find(int id)
	{
		node * finder = head;

		while(finder != NULL)
		{
			if(finder -> data.id == id)
			{
				return finder;
			}

			else
			{
				finder = finder -> next;
			}
		}

		return finder;
	}


Errors:
error C2143: syntax error : missing ';' before '*'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2556: 'int *studentList::find(int)' : overloaded function differs only by return type from 'studentList::node studentList::find(int)'
1> studentList.cpp(57) : see declaration of 'studentList::find'
error C2040: 'studentList::find' : 'int *(int)' differs in levels of indirection from 'studentList::node (int)'

When I change code node * finder = this -> head.

New code and errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	node * studentList::find(int id)
	{
		node * finder = this head;

		while(finder != NULL)
		{
			if(finder -> data.id == id)
			{
				return finder;
			}

			else
			{
				finder = finder -> next;
			}
		}

		return finder;
	}


Errors:
error C2143: syntax error : missing ';' before '*'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2556: 'int *studentList::find(int)' : overloaded function differs only by return type from 'studentList::node *studentList::find(int)'
see declaration of 'studentList::find'
error C2371: 'studentList::find' : redefinition; different basic types
see declaration of 'studentList::find'
error C2440: 'initializing' : cannot convert from 'studentList *const ' to 'studentList::node *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
error C2146: syntax error : missing ';' before identifier 'head'
Last edited on
Could it be that the node * finder is automatically deleted before I can return it since it's created within the function?
Last edited on
So I took the nested class "node" out of the class "studentList". I also made a node constructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class node
{

public:

	student data;
	node * next;
	node * prev;

	node();
};


node::node()
{
	next = NULL;
	prev = NULL;
}


Now the method node * find(int id) works perfect. I just don't understand why? I know it has something to do with me returning an object pointer, because I've used node pointers in my other methods before without ever returning a pointer.
I figured it out, I think. With the original code, where the class "node" was nested in the class "studentList", I had to put studentList::node * as the return type for my function find(int id). I suppose the complier doesn't know what an object pointer "node *" is unless it is told so with the scoping operator. Is this the correct reason?

New code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	studentList::node * studentList::find(int id)
	{
		node * finder = head;

		while(finder != NULL)
		{
			if(finder -> data.id == id)
			{
				return finder;
			}

			else
			{
				finder = finder -> next;
			}
		}

		return finder;
	}



Thanks gdfuison for helping out.
Last edited on
hello again. It does turn out that you were right in that the issue was with the scoping operator; however, I think that you misunderstood me in my initial statement. "this" is just a reference to the current object
ex
studentList s;
if I were to use the s.find() function, the value of "this" would be &s.

Because I was curious, I wrote a program using your code and some of my additions:
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
#include <iostream>



class student
{
public:
	int id;
	student( int sth)
	{
		id = sth;
	}

	student() {}
};


class studentList
{
private:

	class node
	{
	public:

		student data;
		node * next;
		node * prev;
	};

	node * head;

public:
	
	studentList()
	{
		head = nullptr;
	}
	//~studentList();
	void add(student s)
	{
		node * current = this -> head;
		if (current == nullptr)
		{
			head = new node;
			head -> data = s;
			head -> next = nullptr;
			head -> prev = nullptr;
		}
		else
		{
			while( current -> next != nullptr )
			{
				current = current -> next;
			}
			current -> next = new node;
			current -> next -> data = s;
			current -> next -> next = nullptr;
			current -> next -> prev = current;
		}
	}
	void removeStudent(int id);
	//void sort(string field) const;
	void display() const;
	node * find(int id)
	{
		node * finder = this -> head;

		while(finder != NULL)
		{
			if(finder -> data.id == id)
			{
				return finder;
			}

			else
			{
				finder = finder -> next;
			}
		}

		return finder;
	}

};

using namespace std;

int main()
{
	studentList st;
	st.add(student(1));
	st.add(student(3));
	st.add(student(2));
	cout << st.find(3)->data.id << st.find(3)->next->data.id << st.find(3)->prev->data.id;
	getchar();
}

the output was the expected 321.

I then removed the "this" reference from your find function and the output was the same, so I guess that it doesn't matter if you use the this operator or not.

conclusion: your solution was correct; mine was not. Thank you for intriguing me and good luck in the future!

gdfusion
Topic archived. No new replies allowed.