//Cursor.h

#ifndef CURSOR_H
#define CURSOR_H

#include <stdlib.h>
#include <iostream>

template <class Object>
class Cursor;
// Incomplete Declaration

template <class Object>
class CNode
{
        public:

                CNode( const Object & theElement = Object( ), CNode * n = NULL ) : element( theElement ), next( n ) { }
                Object  element;
                CNode	*next;
                friend class Cursor<Object>;
};

template <class Object>
class Cursor
{
	public:
		Cursor( );
		bool isEmpty( ) const;
		void makeEmpty( );
		void left ( );
		void right ( );
		void del ( ); //This is the delete operation. I named it del instead of delete as delete conflicts with a C++ keyword.
		void back ( );
		void insert( const Object & x );
		void home ( );
		void end ( );
		void undo ( );
	private:

		void printText ( ) ;

		CNode<Object> *header;
		CNode<Object> *cursorPosition;
};
//#include "Cursor.cpp"
#endif

//Cursor.cpp

#include "Cursor.h"
#include <iostream>
using namespace std;

template <class Object>
Cursor<Object>::Cursor()
{
    header = new CNode<Object>;
    cursorPosition = header;
}

template<class Object>
void Cursor<Object>::printText()
{
    CNode<Object> *tempPtr;
    if(header == cursorPosition)
       std::cout << "|";

    for(tempPtr=header->next; tempPtr != NULL; tempPtr=tempPtr->next)
    {
        std::cout << tempPtr->element;
        if( tempPtr == cursorPosition )
            std::cout << "|";
    }
    std::cout << "\n\n" ;
}

template <class Object>
bool Cursor<Object>::isEmpty() const
{
   return header->next == NULL;
   std::cout << "The list is empty";
}


template <class Object>
void Cursor<Object>::makeEmpty()
{
    CNode<Object> *tempPtr;

    while(!isEmpty())
    {
            tempPtr = header;
            header = header->next;
            delete tempPtr;
    }
}


  template <class Object>
  void Cursor<Object>::left()
  {
      if( cursorPosition != header )
      {
          CNode<Object> *tempPtr;
          for( tempPtr = header; tempPtr->next != cursorPosition; tempPtr = tempPtr->next );
          cursorPosition = tempPtr;
      }
      //printText();
  }


  template <class Object>
  void Cursor<Object>::right()
  {
      if(cursorPosition->next != NULL)
          cursorPosition = cursorPosition->next;
      //printText();
  }


  template <class Object>
  void Cursor<Object>::del()
  {
      if(cursorPosition->next != NULL)
      {
          CNode<Object> *tempPtr;
          tempPtr = cursorPosition->next;
          cursorPosition->next = tempPtr->next;
          delete tempPtr;
      }
      //printText();
  }

  template <class Object>
  void Cursor<Object>::back()
  {

      if(cursorPosition == header)
          return;
      else
      {
          CNode<Object> *prev, *p2p;   // previous ptr & previous to previous ptr
          for(prev = header; prev->next->next != cursorPosition; prev=prev->next);
          for(p2p = header; p2p->next->next != prev; p2p=p2p->next);
          p2p->next = prev->next;
          cursorPosition = p2p;
          delete prev;
          
      }
  }


  template <class Object>
  void Cursor<Object>::insert(const Object& x)
  {
      CNode<Object> *tempPtr;
      tempPtr = new CNode<Object>;
      tempPtr->element = x;
      tempPtr->next = cursorPosition->next;
      cursorPosition->next = tempPtr;
      cursorPosition = tempPtr;
      //printText();
  }


  template <class Object>
  void Cursor<Object>::home()
  {
      if(cursorPosition == header)
          printText();
      else
          cursorPosition = header;
      printText();
  }

  template <class Object>
  void Cursor<Object>::end()
  {
      for(cursorPosition = header; cursorPosition != NULL && cursorPosition->next != NULL; cursorPosition = cursorPosition->next);
      printText();
  }

//main.cpp

#include <iostream>
#include "Cursor.h"

using namespace std;

/*
 * 
 */
int main()
{
    Cursor<char> text;
    char x;

    cout << "Cursor Navigation options are listed below" << endl;

    cout << "l move left" << endl;
    cout << "r move right" << endl;
    cout << "d deletes character to the right" << endl;
    cout << "b deletes character to the left" << endl;
    cout << "i x - inserts char x after cursor" << endl << endl;


    cin >> x;
    while(x != '$')
    {
        switch(x)
        {
            case 'l':
                text.left();
                break;

            case 'r':
                text.right();
                break;

            case 'd':
                text.del();
                break;

            case 'b':
                text.back();
                break;

            case 'i':
                cin >> x;
                text.insert(x);
                break;

            case 'h':
                text.home();
                break;

            case'e':
                text.end();
                break;

            default:
                cout << "Enter a valid option." << endl;
                break;
        }

        cout << endl << endl;
        cin >> x;
    }

    return 0;
}

Well a little more information about the actual error message and the compile sequence would help.

However, my guess is that you forgot to create an instance of your templated class.
If you combine the code into one file, it works. [Not something to do other than for testing !!!!] . So add line: template class Cursor<char>; at the end
of your cursor.cpp file. This will create an instance of Cursor<char>.

If this doesn't work please post a little of the error messages, and how you are compiling/linking the program.


l

Thank you StuXYZ. That worked. Not exactly why but it did. I thought the instance of my template class would be the job of the constructor.

In addition do you have any ideas as to how to perform an undo action? I am not sure how to go about it.

Thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.