5 Replies - 890 Views - Last Post: 20 December 2014 - 09:45 PM Rate Topic: -----

#1 baruchobama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-December 14

Trying to create a function in a header file and getting two errors.

Posted 20 December 2014 - 06:07 PM

Hey, I am trying to create a function called "divideMid" in my header file linkedListType.h, I am getting two errors:

1)
h:189:6: error: prototype for 'void linkedListType<Type>::divideMid(const linkedListType<Type>&)' does not match any in class 'linkedListType<Type>'
void linkedListType<Type>::divideMid(const linkedListType<Type>& otherList)
^

2)
h:22:13: error: candidate is: void linkedListType<Type>::divideMid(linkedListType<Type>&)
void divideMid(linkedListType<Type> &otherList);
^

my main.cpp is:
#include <cstddef>
#include "unorderedLinkedList.h"

using namespace std;



int main(){

	unorderedLinkedList<int> myList;
	unorderedLinkedList<int> otherList;

	int num;
	cout << "Enter numbers" << endl;

	cin >> num;

	while(num != -999)
	{
		myList.insertLast(num);
		cin >> num;
	}

	cout << "myList = ";
	myList.print();
	myList.divideMid(otherList);
	myList.print();

	myList.initializeList();

	cin.get();
	cin.get();



my unorderedLinkedList.h is:

#ifndef H_unorderedLinkedList
#define H_unorderedLinkedList
#include "linkedListType.h"

using namespace std;

template <class Type>
class unorderedLinkedList: public linkedListType<Type>
{
public:
	bool search(const Type& searchItem) const;
	void insertFirst(const Type& newItem);
	void insertLast(const Type& newItem);
	void deleteNode(const Type& deleteItem);

};

template <class Type>
bool unorderedLinkedList<Type>::search(const Type& searchItem) const
{
	nodeType<Type> *current; 
	bool found = false;
	current = linkedListType<Type>::first;

	while (current != NULL && !found) 
		if (current->info == searchItem)
			found = true;
		else
			current = current->link;
	return found;
}

template <class Type>
void unorderedLinkedList<Type>::insertFirst(const Type& newItem)
{
	nodeType<Type> *newNode;
	newNode = new nodeType<Type>;
	newNode->info = newItem;
	newNode->link = linkedListType<Type>::first;	
	linkedListType<Type>::first = newNode;
	linkedListType<Type>::count++;

	if (linkedListType<Type>::last == NULL)
	linkedListType<Type>::last = newNode;
}

template <class Type>
void unorderedLinkedList<Type>::insertLast(const Type& newItem)
{
	nodeType<Type> *newNode;

	newNode = new nodeType<Type>;
	newNode->info = newItem;
	newNode->link = NULL;

	if (linkedListType<Type>::first == NULL)
	{
		linkedListType<Type>::first = newNode;
		linkedListType<Type>::last = newNode;
		linkedListType<Type>::count++;	 
	}
	else
	{
		linkedListType<Type>::last->link = newNode; 
		linkedListType<Type>::last = newNode;
		linkedListType<Type>::count++;	
	}
}

template <class Type>
void unorderedLinkedList<Type>::deleteNode(const Type& deleteItem)
{
	nodeType<Type> *current; 
	nodeType<Type> *trailCurrent; 
	bool found;

	if (linkedListType<Type>::first == NULL)
		cout << "Cannot delete from an empty list."<< endl;
	else
	{
		if (linkedListType<Type>::first->info == deleteItem) 
		{
			current = linkedListType<Type>::first;
			linkedListType<Type>::first = linkedListType<Type>::first->link;
			linkedListType<Type>::count--;

			if (linkedListType<Type>::first == NULL)	
				linkedListType<Type>::last = NULL;

			delete current;
		}
	else
	{
		found = false;
		trailCurrent = linkedListType<Type>::first;  

		current = linkedListType<Type>::first->link; 

		while (current != NULL && !found)
		{
			if (current->info != deleteItem)
			{
				trailCurrent = current;
				current = current-> link;
			}
			else
				found = true;
		}

		if (found)
		{
			trailCurrent->link = current->link;
			linkedListType<Type>::count--;

			if (linkedListType<Type>::last == current)  

				linkedListType<Type>::last = trailCurrent; 

			delete current;  
		}
		else
			cout << "The item to be deleted is not in the list." << endl;
		}
	}
}

#endif



my linkedListType.h is:

#include "linkedListIterator.h"
#include <iostream>
#include <cassert>

using namespace std;

template <class Type>
class linkedListType
{
public:
       const linkedListType<Type>& operator=(const linkedListType<Type>&);   
       void initializeList();
       bool isEmptyList() const; 
       void print() const; 
       int length() const;
       void destroyList();
       void divideMid(linkedListType<Type> &otherList);
       Type front() const;
       Type back() const;
       virtual bool search(const Type& searchItem) const = 0;
       virtual void insertFirst(const Type& newItem) = 0;
       virtual void insertLast(const Type& newItem) = 0;
       virtual void deleteNode(const Type& deleteItem) = 0;
       linkedListIterator<Type> begin();
       linkedListIterator<Type> end();
       linkedListType();
       linkedListType(const linkedListType<Type>& otherList);
       ~linkedListType();
       
protected:
          int count;
          nodeType<Type> *first;
          nodeType<Type> *last;

private:
        void copyList(const linkedListType<Type>& OtherList);
        
};

template <class Type>
bool linkedListType<Type>::isEmptyList() const
{
  return (first == NULL);
}

template <class Type>
linkedListType<Type>::linkedListType() 
{
  first = NULL;
  last = NULL;
  count = 0;
}

template <class Type>
void linkedListType<Type>::destroyList()
{
  nodeType<Type> *temp;

  while (first != NULL)  
  {
    temp = first;  
    first = first->link; 
    delete temp;  
  }
  last = NULL; 
  count = 0;
}

template <class Type>
void linkedListType<Type>::initializeList()
{
  destroyList(); 
}

template <class Type>
void linkedListType<Type>::print() const
{
  nodeType<Type> *current;
  current = first;  

  while (current != NULL)
  {
    cout << current->info << " ";
    current = current->link;
  }
}
             
template <class Type>
int linkedListType<Type>::length() const
{
  return count;
}

template <class Type>
Type linkedListType<Type>::front() const
{
  assert(first != NULL);
  return first->info;
}

template <class Type>
Type linkedListType<Type>::back() const
{
  assert(last != NULL);
  return last->info; 
}

template <class Type>
linkedListIterator<Type> linkedListType<Type>::begin()
{
  linkedListIterator<Type> temp(first);
  return temp;
}

template <class Type>
linkedListIterator<Type> linkedListType<Type>::end()
{
  linkedListIterator<Type> temp(NULL);
  return temp;
}

template <class Type>
void linkedListType<Type>::copyList(const linkedListType<Type>& otherList)
{
  nodeType<Type> *newNode;
  nodeType<Type> *current;
  if (first != NULL)
    destroyList();
  if (otherList.first == NULL)
  {
    first = NULL;
    last = NULL;
    count = 0;
  }
  else
  {
    current = otherList.first;
    count = otherList.count;
    
    first = new nodeType<Type>;
    first->info = current->info; 
    first->link = NULL;  
    last = first;  
    current = current->link;
    while (current != NULL)
    {
      newNode = new nodeType<Type>;  
      newNode->info = current->info;
      newNode->link = NULL; 

      last->link = newNode;  
      last = newNode;

      current = current->link;  

      }
   }
}

template <class Type>
linkedListType<Type>::~linkedListType() //destructor
{
  destroyList();
}

template <class Type>
linkedListType<Type>::linkedListType(const linkedListType<Type>& otherList)
{
  first = NULL;
  copyList(otherList);
}

template <class Type>
const linkedListType<Type>& linkedListType<Type>::operator=(const linkedListType<Type>& otherList)
{
  if (this != &otherList)
  {
    copyList(otherList);
  }
   return *this;
}

template <class Type> 
void linkedListType<Type>::divideMid(const linkedListType<Type>& otherList)
{
  int myListItems, otherListItems;
  if ((count%2)!=0) myListItems = (count/2 + 1);
  else myListItems = (count/2);
  otherListItems = (count - myListItems);

  nodeType<Type> *current;
  current = first;
  otherList.last = last;
  for (int i=0; i<myListItems; i++) 
  {
    last = current; //sets the last node of the first list
    current = current -> link; //traverses the list until it gets to where it must divde. 
  }
  last->link=NULL;  //cuts off myList in the middle
  otherList.first = current;  //assigns the next node to sublist.first. 
}

#endif 


^^^^^^^^^^^That is the file that says I am having an error.
Finally my linkedListIterator.h is:

#ifndef H_linkedListIterator
#define H_linkedListIterator

#include <iostream>


template<class Type> struct nodeType
{
	Type info;
	nodeType<Type> *link;
};


template <class Type>
class linkedListIterator
{
public:
	linkedListIterator();
	linkedListIterator(nodeType<Type> *ptr);
	Type operator*(); 
	linkedListIterator<Type> operator++();
	bool operator==(const linkedListIterator<Type>& right) const;
	bool operator!= (const linkedListIterator<Type>& right) const;

private:
		nodeType<Type> *current;
	
};

template <class Type>
linkedListIterator<Type>::linkedListIterator()
{
	current = NULL;
}

template <class Type>
linkedListIterator<Type>::linkedListIterator(nodeType<Type> *ptr)
{
	current = ptr;
}

template <class Type>
Type linkedListIterator<Type>::operator*()
{
	return current->info;
}

template <class Type>
linkedListIterator<Type> linkedListIterator<Type>::operator++()
{
	current = current->link;
	return *this;
}

template <class Type>
bool linkedListIterator<Type>::operator==(const linkedListIterator<Type>& right) const
{
	return (current == right.current);
}
		
template <class Type>
bool linkedListIterator<Type>::operator!=(const linkedListIterator<Type>& right) const
{
	return (current != right.current);
}

#endif



If you could point me in the right direction that would be awesome or if its a syntax error and you find it I would be really grateful too.

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to create a function in a header file and getting two errors.

#2 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,525
  • Joined: 12-December 12

Re: Trying to create a function in a header file and getting two errors.

Posted 20 December 2014 - 06:29 PM

What is your interpretation of the error message? What is it telling you?
Was This Post Helpful? 2
  • +
  • -

#3 baruchobama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-December 14

Re: Trying to create a function in a header file and getting two errors.

Posted 20 December 2014 - 07:04 PM

I cant remember exactly what its called so bear with me but I think it has something to do with an improper inheritance and that the program has no idea what sublist is because its not initialized in the linkedListType file and only initialized in the main?
Was This Post Helpful? 0
  • +
  • -

#4 baruchobama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-December 14

Re: Trying to create a function in a header file and getting two errors.

Posted 20 December 2014 - 08:41 PM

Am I headed in the right direction or should I think about something else?
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Trying to create a function in a header file and getting two errors.

Posted 20 December 2014 - 08:59 PM

No, the answer is simpler.

A prototype is composed of the function name and parameters and return type.

Have a look at how you construct the function head in the class, and how you defined it where you write the function.
Was This Post Helpful? 1
  • +
  • -

#6 baruchobama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-December 14

Re: Trying to create a function in a header file and getting two errors.

Posted 20 December 2014 - 09:45 PM

Thanks a bunch, I think I just needed to take a step back and stop thinking the problem was bigger than it was.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1