Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Confused on simple error (http://www.go4expert.com/forums/confused-simple-error-t26602/)

Tinymittenz 2Sep2011 02:03

Confused on simple error
 
It seems I have been looking at this code for hours on in and I feel lost in the woods. I know it might be such an easy simple error to correct, but I cant seem to figure it out. Any help will be greatly appreciated.

Code:


#include
<stdlib.h>
#include<string>
#include<iostream>
using std::cout;
using std::string;
// node object for the linked list
struct Node {
int data;
Node* link;
};
// implement a singly linked list
class LinkedList {protected:
Node* front; // pointer to the front of the linked list
Node* back; // pointer to the last node in the linked list

public:
// constructs an empty list
LinkedList() {
front = back = NULL;
}
// deletes the list
~LinkedList() {
// remove objects from the list as long as list is not empty
while(Length() > 0) {
RemoveFront();
}
}
 
// inserts a node at the front of the list
void InsertFront(int newValue) {
Node* newNode = new Node;
newNode->data = newValue;
if (front == NULL) {
// list must be empty so make front & back point to new node
front = back = newNode;
newNode->link = NULL;
} else {
// list is not empty so insert between front and first node
newNode->link = front;
front = newNode;
}
}
// removes a node from the front of the list
int RemoveFront() {
int returnVal;
Node *temp;
if (front != NULL) {
// list is not empty so remove & return first node
returnVal = front->data;
temp = front;
front = front->link;
} else {
// list is empty just return 0
returnVal = 0;
}
return returnVal;
}
// returns the length of the list
int Length() {
Node* p;
int count = 0;
// loop through each node in the list until we find a null value
for (p = front; p != NULL; p = p->link) {
count++;
}
return count;
}
// outputs a string containing all the data values in the list
void Output() {
Node* p;
// loop through each node in the list until we find a null value
for (p = front; p != NULL; p = p->link) {
cout << p->data << ", ";
}
}
//use inheritance to create a Set Cclass from the LinkedList class
class Set: public LinkedList {
//insert a new value only if it is unique (not already in the set)
public:
void Insert(int newValue){
int duplicate = Search(newValue);
if (duplicate == -1)
InsertFront(newValue);
}
 
 
//make this union of two sets
void Union(Set& a, Set& B){
Node*p;
for (p = a.front; p != NULL; p = p->link){
Insert(p->data);
}
for (p = b.front; p != NULL; p = p->link){
Insert(p->data);
}
}
//make this intersection of two sets
void Intersection(Set& a, Set& B){
Node* p;
Node* q;
for (p = a.front; p != NULL; p = p->link)
{
for (q = b.front; q != NULL; q = p->link)
{
if (p->data == q->data)
{
Insert(p->data);
}
}
}
}
};
void main (){
Set setA, setB, setUnion, setIntersection;
setA.Insert(5);
setA.Insert(2);
setA.Insert(3);
setA.Insert(5);
setA.Insert(2);
cout << "Contents of setA: ";
setA.Output();
cout << "\n\n";
setB.Insert(1);
setB.Insert(2);
setB.Insert(4);
cout << "Contents of setB: ";
setB.Output();
cout << "\n\n";
setUnion.Union(setA, setB);
cout << "Contents of setA union setB: ";
setUnion.Output();
cout << "\n\n";
setIntersection.Intersection(setA, setB);
cout << "Contents of setA intersection setB: ";
setIntersection.Output();
cout << "\n\n";
system("PAUSE");
}

This is my error: error C2504: 'LinkedList' : base class undefined

priyatendulkar 2Sep2011 11:12

Re: Confused on simple error
 
hi,

U have declared class LinkedList incorrectly..
Add the following closing brace and semiclon at the end of the class
ie after
void Output() {
Node* p;
// loop through each node in the list until we find a null value
for (p = front; p != NULL; p = p->link) {
cout << p->data << ", ";
}
}

}; //add this....


All times are GMT +5.5. The time now is 16:14.