Help Understanding Vague Assignment

Pages: 12
I'm assigned to implement a linked list with the help of an external iterator class. I'm finding the assignment to be difficult and from what I can see, it's missing pieces of code. The header files were given by the teacher and we have to implement them. Can't use standard library.

DRIVER PROVIDED BY TEACHER
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// FILE: sequence_test.cxx
// An interactive test program for the new sequence class
#include <cctype>       // Provides toupper
#include <iostream>     // Provides cout and cin
#include <cstdlib>      // Provides EXIT_SUCCESS
#include "list.h"  // With value_type defined as int

#include "Iterator.h"  // ERROR1: PCH warning: header stop not at file scope.  
                       // An Intellisense PCH file was not generated.

// Don't really understand the error. When I created the blank project, I added // all the files given to me by my teacher.	

using namespace std;
using namespace list_1;


List.h File Class is implemented after the header portion.
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
  #ifndef LIST_H
#define LIST_H
#include "Node.h"
#include "Iterator.h"

namespace list_1
{
	class list
    {
    public:
        // CONSTRUCTOR
        list( );

		// postcondition: all nodes in the list are destroyed.
		~list();
        
		// MODIFICATION MEMBER FUNCTIONS
		
		//postcondition: entry is added to the front of the list
        void insert_front(const int& entry);
        
		//postcondition: entry is added to the back of the list
        void add_back(const int& entry);
		
		// postcondition: all nodes with data == entry are removed from the list
        void remove_all(const int& entry);
		
		// postcondition: an iterator is created pointing to the head of the list
		Iterator begin(void);
		
		// CONSTANT MEMBER FUNCTIONS
		
		// postcondition: the size of the list is returned
        int size( ) const;

    private:
		Node* head;
	};

// IMPLEMENTATION OF LIST CLASS
	
//Default Constructor
list::list()
{
	head ==NULL;
}

list::~list()
{
	Node *p = head;
	while(p != NULL)
	{
		Node *pnext =p->next;
		delete p;
		p=pnext;
	}
}

void list::insert_front(const int& entry)
{
	if(head==NULL)
	{
		Node *nn=new Node(entry);
		return;
	}
	Node *nn=new Node(entry);
	nn->next=head;
	head=nn;
}

void list::add_back(const int& entry)
{
	if(head==NULL)
	{
		Node *nn=new Node(entry);
		return;
	}
	Node *temp = head;
	while(temp->next != NULL)
	{
		temp = temp->next;
	}
	temp->next=new Node(entry);
}

void list::remove_all(const int& entry)
{
	// Haven't started this part yet
}

// postcondition: an iterator is created pointing to the head of the list
Iterator begin(void)
{
	Iterator I(Node head)
        return I;    //	ERROR2: no suitable constructor exists to convert
                     // from "list_1::Iterator (list_1::Node head)" to 
                     // "list_1::Iterator"
}

// postcondition: the size of the list is returned
int size() const
{

}
#endif 


Iterator.h file. Class implemented after header portion
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
// Template CLASS PROVIDED: Iterator 

#pragma once
#include "Node.h"

namespace list_1
{
	class Iterator
	{
	public:
		// Constructor
		Iterator (Node *np);

		// precondition: is_item is true
		// post condition n points to the next item in the list
		void operator++();
		
		// precondition: 
		// postcondition: returns true if there is a valid item
		bool is_item();
		
		// precondition: is_item == true
		// postcondition returns data that n is pointing at
		int operator* ();

	private:
		Node* n;
	};

// IMPLEMENTATION CODE FOR ITERATOR CLASS

	// Constructor
	Iterator::Iterator(Node *np)
	{
		n=np;
	}

	// Operator ++ Overload Function
	void Iterator::operator++()
	{
		if(is_item())
		{
			n++;
		}
	}

	// precondition: is_item == true
	// postcondition returns data that n is pointing at
	int Iterator::operator* ()
	{
		if(is_item())
		{
			return n;   // ERROR3: return value type does not match  
                                    // the function type

		}
		else
		{
			return -1;
		}
		
	}
        // purpose: checks current position for valid item
        // postcondition: returns true if there is a valid item
	bool is_item()
	{
		return np !=NULL;   //ERROR4: np is listed as undefined. 
	}
}


and here's my Node class for reference:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once

namespace list_1
{
	struct Node
	{
		// Constructor
		// Postcondition: 
		Node (int d);

		// Variable Data
		int data;
		Node *next;
	};

// IMPLEMENTATION OF NODE CLASS

	Node::Node(int d)
	{
		data = d;
		next = NULL;
	}
}


Any form of help would be greatly appreciated.
Can't use standard library.
1
2
3
4
5
6
7
#include <cctype>
#include <iostream>
#include <cstdlib>

// ...

using namespace std;

Huh?
This is what the teacher has placed on the assignment:
You may not use the standard template library for this or any other project this semester unless specifically mentioned in the instructions


Pretty much states that I can't use any standard pre-built template within any code I write (The List.h, Node.h, Iterator.h). It has to be written out. The driver wasn't written by me. That came straight from the teacher.
Last edited on
I see.

What's your problem? You only say the assignment is difficult, and in the code, I only see line 8 throws a warning.
Sorry I've been picking at it a piece at a time. Guess I'll just start here.
1
2
3
4
5
6
7
8
// postcondition: an iterator is created pointing to the head of the list
Iterator begin(void)
{
	Iterator I(Node head)
        return I;    //	ERROR2: no suitable constructor exists to convert
                     // from "list_1::Iterator (list_1::Node head)" to 
                     // "list_1::Iterator"
}


Post Condition states what Iterator begin(void) is suppose to do. I'm thinking that I've probably gotten the code all wrong. We did something similar in class but that didn't involve a linked list like this one does.
typename1 identifier1 ( typename2 identifier2 );

That is a declaration of a function that takes one parameter (and yours is missing a semicolon too).

Try Iterator I( head );
1
2
Iterator I(head);
        return I;


Now it's saying that head is undefined.
How can head be undefined when list declares it as a private field? Where are you defining begin?
You're missing the class name and the scope operator on that function definition:
1
2
3
4
5
Iterator list::begin(void)
{
	Iterator I(head)
        return I;  
}
LOL! Maybe I should take a break. Been working on this thing for about 4 hrs straight.
Working that long is counterproductive, but I know how it feels. Relax!
PCH warning: header stop not at file scope. An intellisense PCH file was not generated.

Can anyone explain what this means?

It's the only thing that comes up when I try to debug my code but it prevents it from building the program. Any ideas on how to resolve it?
Last edited on
sheesh, I would just write my own class to handle a linked list I write... this is just... dumb...

Did your professor tell you that you had to use the provided header files?

Also, the beginning/ending nodes would be better initiated at construction and deleted at destruction (along with all the other nodes)... and your professor wrote this??

I also wouldn't even use an iterator... next() and prev() would be functions I write to handle the task... or better yet just overload operators for that.
Last edited on
Either he wrote it or he got it from our textbook. I recognize parts of it that are from the book.

Yeah we have to follow what's listed in the header files and implement the code.

Is there an error with my destructor? Is that what you mean by your last statement. (Confused)
yeah I did a linked list in my CS1410 class in the spring and that one was less confusing than this one. It's like code has been omitted on purpose/forgotten. But we can't anything more/less than what's stated in the class definitions. Hence why I've called this blog Vague Assignment.
Where do you study? UHD?
Utah Valley University. We're using Data Structures and Other Objects Using C++ by Michael Main & Walter Savitch.
Getting this:

no suitable constructor exists to convert from "list_1::Iterator (list_1::Node head)" to "list_1::Iterator"

1
2
3
4
5
Iterator list::begin(void)
	{
		Iterator I(Node head);
		return I;  // It's coming up on the I
	}


I had to make an empty project and rewrite everything to get rid of the previously mentioned PCH error/warning. Stupid crap!
This happens to me a few times. Iterator I(Node head); is a function prototype and not a call to the constructor. The compiler thought you were trying to returna function pointer.

This is because you wrote the type name "Node" in the call to the constructor. So it was like you were declaring the parameter list of the function and not passing arguments.
Pages: 12