我无法让我的Partial Arrays类进行编译。在我的一个成员函数中,我调用了一个前一个成员函数,并得到错误"成员引用基类型' ITEM_TYPE [255]'不是结构或联盟"。
我不完全确定我的成员变量声明是否符合要求,因为这是我第一次使用C ++处理数组。
这是我的标题:
#ifndef PARTIALARRAY_H
#define PARTIALARRAY_H
#include <iostream>
#include <string.h>
using namespace std;
typedef int ITEM_TYPE;
ITEM_TYPE const MAX = 255;
class PartialArray
{
public:
//-----------------------------------------ctors:-----------------------------------------
PartialArray();
PartialArray(ITEM_TYPE MAX, int numUsed);
//-----------------------------------------member functions:-----------------------------------------
void PrintArray(int a[], int numUsed);
int Search(int a[], int numUsed, ITEM_TYPE key);
int Append(ITEM_TYPE appendMe);
int ShiftRight(int shiftHere);
int ShiftLeft(int shiftHere);
int InsertBefore(ITEM_TYPE insertThis, int insertHere);
int InsertAfter(ITEM_TYPE insertThis, int insertHere);
int Delete(int deleteHere);
string ErrorDescr(int failCode);
private:
//-----------------------------------------member vars:-----------------------------------------
ITEM_TYPE a[MAX];
int numUsed;
};
#endif // PARTIALARRAY_H
我的类声明(注意:错误函数和返回值不完整,因此可以忽略):
#include "partialarray.h"
#include <iostream>
#include <string.h>
using namespace std;
//-----------------------------------------ctors:-----------------------------------------
PartialArray::PartialArray()
{
numUsed=0;
}
PartialArray::PartialArray(ITEM_TYPE MAX, int numUsed)
{
numUsed = MAX;
}
//-----------------------------------------member functions:-----------------------------------------
//Prints the array up to its last used element
void PartialArray::PrintArray(ITEM_TYPE a[], int numUsed)
{
for(int i=0; i<numUsed; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
//Searches the array for a particular value and returns the index at which the value first appears
int PartialArray::Search(ITEM_TYPE a[], int numUsed, ITEM_TYPE key)
{
for(int i=0; i<numUsed; i++)
{
if(a[i]==key)
{
return i;
break;
}
else
;
}
return -1; //placeholder for error
}
//Takes a number and appends it to the end of the array after the last interesting element
int PartialArray::Append(ITEM_TYPE appendMe)
{
if(a[numUsed==0])
a[numUsed] = appendMe;
else
return 0; //placeholder for error
return 1; //placeholder for error
}
//Shifts all elements of the array to the right starting at a particular index
int PartialArray::ShiftRight(int shiftHere)
{
ITEM_TYPE save = a[numUsed-1];
for(int i=numUsed; i>=shiftHere; i--)
{
a[i] = a[i-1];
}
a[0] = save;
return 1; //error placeholder
}
//Shifts all elements of the array to the left starting at a particular index
int PartialArray::ShiftLeft(int shiftHere)
{
ITEM_TYPE save = a[0];
for(int i=shiftHere; i<numUsed; i++)
{
a[i] = a[i+1];
}
a[numUsed-1] = save;
return 1; //error placeholder
}
//Takes a number and a position and inserts the number at that position in the array shifting the elements to the right
int PartialArray::InsertBefore(ITEM_TYPE insertThis, int insertHere)
{
a.ShiftRight(insertHere);
a[insertHere] = insertThis;
return 1; //error placeholder
}
//Takes a number and a position and inserts the number at that position in the array shifting the elements to the left
int PartialArray::InsertAfter(ITEM_TYPE insertThis, int insertHere)
{
a.ShiftLeft(insertHere);
a[insertHere] = insertThis;
return 1; //error placeholder
}
//Takes a position and removes that item from the array, shifting all the elements to the left
int PartialArray::Delete(int deleteHere)
{
a[deleteHere] = 0;
a.ShiftLeft(deleteHere);
return 1; //error placeholder
}
string PartialArray::ErrorDescr(int failCode)
{
switch(failCode)
{
case 1:
return "ERROR: etc";
break;
case 2:
return "ERROR: etc";
break;
case 3:
return "ERROR: etc";
break;
case 4:
return "ERROR: etc";
break;
case 5:
return "ERROR: etc";
break;
default:
return "ERROR: etc";
break;
}
}
我之前已经构建了一个Rationals类,我觉得这个材料很稳固,但是做类似于数组的事情已被证明是一个令人头痛的问题。任何帮助将不胜感激!
答案 0 :(得分:0)
您的大多数代码似乎都可以。但是,a
是一个C风格的数组,因此没有成员函数。
因此,以下行不正确:
a.ShiftRight(insertHere);
...
a.ShiftLeft(insertHere);
当您尝试将此数组的语法仅用于结构或类变量时,编译器会在此处发出您观察到的错误消息:"member reference base type 'ITEM_TYPE [255]' is not a structure or union"