Full disclosure, this is an assignment. I just need help figuring out how to fix my errors. And I apologize for the relatively messy code and badly-named variables (these will be fixed). I have spent many hours staring at it this week and I just can't seem to figure it out. We had to convert a single-line text editor which uses arrays to one that uses OOP and double-linked lists and I have been doing it in steps. I have, for the sake of convenience, put my headers, implementation and main all in one file.
I'm compiling this program in Hercules (the getch function uses C code).
I keep getting the error from the compiler saying "Undefined Symbol" for functions:
insertNode(char, Node);
deleteNode(Node*);
insertHead(char);
The full message is: ld: fatal: Symbol Referencing Errors. No output written to a.out collect2: ld returned with 1 exit status.
// A fake single line text editor program.
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <termios.h>
//#include "main.h"
typedefchar Letter;
usingnamespace std;
/****************************************************************************/
struct Node
{
Letter m_letter;
Node *next;
Node *prev;
friendclass dblyLinked;
};
class dblyLinked
{
public:
dblyLinked();
int insertHead(Letter);
bool isFull();
bool isEmpty();
int moveRight();
int moveLeft();
int insertSorted(Letter, Node*);
int resetP();
int deleteNode(Node*);
void printList();
int setP(Node*);
private:
Node *head;
Node *tail;
Node *p;
int length;
};
/****************************************************************************/
Letter getch();
int editor();
int main()
{
return editor();
}
/**************************************************************************/
int editor()
{
int insertHead(Letter);
bool isFull();
bool isEmpty();
int moveRight();
int moveLeft();
int insertSorted(Letter, Node);
int resetP();
int deleteNode(Node*);
void printList();
int setP(Node*);
Letter c;
int i; // KEEP
int cursorPosition = 1; // KEEP
int length = 0;
Node *tempPtr;
try
{
tempPtr = new Node;
}
catch (bad_alloc)
{
std::cerr << "Bad Memory Allocation!";
delete tempPtr;
}
Node *head = new Node;
Node *tail = new Node;
Node *currPtr = new Node;
Node *p = head;
system("clear");
cout << " " << endl;
cout << "^" << endl;
while (1)
{
c = getch();
// The Backspace key was pressed.
if (c == 8)
{
if (length > 0)
{
Node *tempPtr = currPtr;
deleteNode(tempPtr->prev);
length--;
cursorPosition--;
}
}
// One of the cursor control keys was pressed.
elseif (c == 27)
{
c = getch();
c = getch();
if (c >= 49 && c <= 54)
{
// The Home key was pressed.
if (c == 49)
{
p = head;
cursorPosition = 1;
}
// The Delete key was pressed.
elseif (c == 51)
{
Node *tempPtr = currPtr;
deleteNode(tempPtr);
length--;
}
// The End key was pressed.
elseif (c == 52)
{
p = tail;
cursorPosition = length+1;
}
// This gets rid of a character in the input stream
c = getch();
}
// The Move Cursor Right key was pressed.
elseif (c == 67)
{
int moveRight();
cursorPosition++;
}
// The Move Cursor Left key was pressed.
elseif (c == 68)
{
int moveLeft();
cursorPosition--;
}
}
elseif (c > 31 && c < 127) // WHERE STUFF GETS INSERTED!
{
currPtr->m_letter = c;
if (p == head)
insertHead(c);
else
insertSorted(c, *currPtr);
length++;
cursorPosition++;
}
system("clear");
void printList();
// Print text [] to the screen.
i = 1;
cout << endl;
if (c == '.' || c == '?' || c == '!') // node->c ==.....
{
break;
}
// Position the cursor.
i = 1;
while (i < cursorPosition)
{
i++;
cout << " ";
}
cout << "^" << endl;
}
return 0;
}
I'm confused; are you saying that I should not use forward declarations?
Anyway, another problem was the syntax of the function calls. insertSorted(c, *currPtr)SHOULD have been insertSorted(char c, Node *currPtr) and so on. Ah well. I couldn't find a segmentation fault in time for the deadline, but I sure know what I need to work on! Thanks for your help either way kbw. :)
.. are you saying that I should not use forward declarations?
Yes. You should be using prototypes.
Look at it this way. Some creates a library with functions that do something. They provide declarations (data types and function prototypes) in header files, and a library to link against.
The correct way to use such a library is to include the given header files to get declarations, and link against the libraries to get the code.
An incorrect way to uise such a library is to guess what a function might look like, create your own definition of that function, and use it.
That's what you've done with getch. It's an curses function, so you should pull in the relevant header file #include <curses.h> , and link against libcurses; or whatever your system provides.