c++ - 在 C++ 中构建简单的部分数组类时出现编译时错误

标签 c++ arrays class private member

我在编译部分数组类时遇到问题。在我的一个成员函数中,我调用了以前的成员函数并收到错误“成员引用基类型‘ITEM_TYPE [255]’不是结构或 union ”。

我不完全确定我的成员变量声明是否符合要求,因为这是我第一次在 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 类,我对这些 Material 感觉很扎实,但是用数组做类似的事情被证明是一个令人头疼的问题。任何帮助将不胜感激!

最佳答案

你的大部分代码看起来都不错。但是,a 是 C 风格的数组,因此没有成员函数。

因此,以下几行是不正确的:

a.ShiftRight(insertHere);
...
a.ShiftLeft(insertHere);

当您尝试使用仅对结构或类变量有意义的语法使用此数组时,编译器会在此处发出您观察到的错误消息:"member reference base type 'ITEM_TYPE [255]' is not a structure or union ”

关于c++ - 在 C++ 中构建简单的部分数组类时出现编译时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33515785/

相关文章:

c++ - Windows - VC++ - 不能在静态构建中使用 "_ASSERTE"

java - 为什么 Java 读取大文件的速度比 C++ 快?

c++ - 为什么大小不是 std::initializer_list 的模板参数?

C++使用递归查找Vector中非零的数量

ruby - 包含数组的散列到 ruby​​ 中的散列数组

c# - 在 C# 的数组列表中查找数组的索引

javascript - Polymer JS 从 JSON 文件读取

c++ - C++ 中前向声明的类

java - Java:数组操作函数中的错误

c# - 从字符串创建类实例