Reputation: 402
first of all, I realise that there is already a class for a dynamic array in C++. I am doing this out of experimentation, not neccecity. I have a class called DynamicCharArray. My intention is to implement a dynamic array by using a linked list. It contains several methods, some private and some public. Two of these are:
void InterInsert(node *&head, node *&last, int index, char input)
and
void insert(int index, char input).
InterInsert(...) contains all the code to actually insert the character into my array. insert(...) is an abstraction.
My questions are: Am I on the right track, and if not, how should I implement this? Why am I getting the following errors?:
error: expected primary-expression before '*' token this -> InterInsert(node *&head, node *&last, int index, char input);
error: 'head' was not declared in this scope this -> InterInsert(node *&head, node *&last, int index, char input);
error: expected primary-expression before '*' token this -> InterInsert(node *&head, node *&last, int index, char input);
error: 'last' was not declared in this scope this -> InterInsert(node *&head, node *&last, int index, char input); error: expected primary-expression before 'int' this -> InterInsert(node *&head, node *&last, int index, char input);
error: expected primary-expression before 'char' this -> InterInsert(node *&head, node *&last, int index, char input);
This program is still in it's early stages. But I would dearly like to fix these errors before continuing. Many methods may be added in the future, suggestions on this would be greatly appreciated.
in DynamicCharArray.h:
#ifndef DYNAMICCHARARRAY_H
#define DYNAMICCHARARRAY_H
class DynamicCharArray
{
private:
struct node
{
char let;
node *next;
int index;
};
int size;
bool isEmpty(node *head);
void InterInsert(node *&head, node *&last, int index, char input);
void insertAsFirstElement(node *&head, node *&last, char input);
public:
DynamicCharArray(); //create empty array
void insert(int index, char input); //insert an element into array
void printAll(); //print all data values contained in the array
char index(int numIn); //returns the char value in a particular index. Same effect as: myArray[numIn] instead I will call it as: myChar = myDynamicCharArray.index(5)
~DynamicCharArray();
};
#endif // DYNAMICCHARARRAY_H
in DynamicCharArray.cpp:
#include "dynamicchararray.h"
#define NULL 0
struct node
{
char let;
node *next;
int index;
};
int size;
DynamicCharArray::DynamicCharArray() //create empty array
{
node *head = NULL;
node *last;
node list;
size = 0;
}
bool DynamicCharArray::isEmpty(node *head)
{
if(head == NULL)
{
return true;
}
else
{
return false;
}
}
void DynamicCharArray::InterInsert(node *&head, node *&last, int index, char input)
{
if(isEmpty(head))
{
insertAsFirstElement(head, last, input);
}
else
{
node *newNode = new node; //new node is dynamicly created (space created)
newNode -> let = input; //value that is passed is assigned to new node
newNode -> next = last -> next; //the new node's pointer gets the same value as the last pointer, as it is now in the 'place' where last was.
last -> next = newNode; //The new node is now the last, and the last node now points to it
last = last -> next; //make last, point to the last element in the list
}
}
void DynamicCharArray::insertAsFirstElement(node *&head, node *&last, char input) //insert as first element function from standard linked list program
{
node *newNode = new node;
newNode -> index = 0;
newNode -> let = input;
newNode -> next = NULL; //Here is the difference between the first element, and the rest. Here the tail/last pointer to node does not have a value yet. It will get this node's value in the rest of this function.
head = newNode;
last = newNode;
}
void DynamicCharArray::insert(int index,char input) //insert an element into array
{
InterInsert(node *&head, node *&last, int index, char input); //HERE IS THE LINE THE ERRORS ARE REFERING TO
}
char DynamicCharArray::index(int numIn) //returns the char value in a particular index. Same effect as: myArray[numIn] instead I will call it as: myChar = myDynamicCharArray.index(5)
{
}
DynamicCharArray::~DynamicCharArray()
{
}
in main.cpp:
. . .
DynamicCharArray myDynamicCharArray1 = DynamicCharArray();
myDynamicCharArray1.insert(0,'a');
. . .
Thank you for reading!
Upvotes: 0
Views: 108
Reputation: 626
To simply answer your question, it happens because of a syntax error while you're calling the function InterInsert:
void DynamicCharArray::insert(int index,char input) //insert an element into array
{
InterInsert(node *&head, node *&last, int index, char input);
}
In this case you have a prototype definition inside a function, which is illegal. To actually perform a function call, do this instead:
void DynamicCharArray::insert(int index, char input) //insert an element into array
{
node* head = new node; // you should probably edit these...
node* last = new node;
InterInsert(head, last, index, input);
}
Upvotes: 0