首先,我意识到C ++中已有一个动态数组类。我这样做是出于实验,而不是真理。我有一个名为DynamicCharArray的类。我的目的是使用链表实现动态数组。它包含几种方法,一些是私有的,一些是公共的。其中两个是:
void InterInsert(node *&head, node *&last, int index, char input)
和
void insert(int index, char input).
InterInsert(...)包含将字符实际插入到我的数组中的所有代码。 insert(...)是一个抽象。
我的问题是: 我是否在正确的轨道上,如果没有,我应该如何实现这一目标? 为什么我会收到以下错误?:
错误:在' *'之前预期的主要表达式代币 这个 - > InterInsert(node *& head,node *& last,int index,char input);
错误:&#39> head'在这方面没有申明 这个 - > InterInsert(node *& head,node *& last,int index,char input);
错误:在' *'之前预期的主要表达式代币 这个 - > InterInsert(node *& head,node *& last,int index,char input);
错误:'最后'在这方面没有申明 这个 - > InterInsert(节点*& head,node *& last,int index,char input); 错误:在' int'之前预期的primary-expression; 这个 - > InterInsert(node *& head,node *& last,int index,char input);
错误:在' char'之前预期的primary-expression 这个 - > InterInsert(node *& head,node *& last,int index,char input);
该计划仍处于初期阶段。但我非常希望在继续之前解决这些错误。将来可能会添加许多方法,对此的建议将不胜感激。
在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
在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()
{
}
在main.cpp中:
。 。
DynamicCharArray myDynamicCharArray1 = DynamicCharArray();
myDynamicCharArray1.insert(0,'a');
。 。
感谢您阅读!
答案 0 :(得分:0)
要简单回答您的问题,当您调用InterInsert函数时会发生语法错误:
void DynamicCharArray::insert(int index,char input) //insert an element into array
{
InterInsert(node *&head, node *&last, int index, char input);
}
在这种情况下,您在函数内部有一个原型定义,这是非法的。要实际执行函数调用,请执行以下操作:
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);
}