锯齿状的数组

时间:2012-06-18 22:28:04

标签: c++ jagged-arrays

在我的新工作中,我意识到我到目前为止实际上使用的C ++技能有多少。我现在正在努力通过一些自我强加的OOP练习,而且我被困在一堆锯齿状的课程上。

这是代码:

#define CAT_ONE_COUNT    6
#define CAT_TWO_COUNT    7
#define CAT_THREE_COUNT 20
#define CAT_FOUR_COUNT   5
#define CAT_FIVE_COUNT   4
#define CAT_SIX_COUNT   20

enum {CAT_ONE, CAT_TWO, CAT_THREE, CAT_FOUR, CAT_FIVE};

class EntryList
{
    private:
        ScheduleEntry* catOne[CAT_ONE_COUNT];
        ScheduleEntry* catTwo[CAT_TWO_COUNT];
        ScheduleEntry* catThree[CAT_THREE_COUNT];
        ScheduleEntry* catFour[CAT_FOUR_COUNT];
        ScheduleEntry* catFive[CAT_FIVE_COUNT];
        ScheduleEntry* catSix[CAT_SIX_COUNT];

        ScheduleEntry** entries[];

    public:
        EntryList();
        ~EntryList();

        std::string getEntry(int cat, int entry);
};

EntryList::EntryList()
{
    catOne[0] = new ScheduleEntry("Pressups");
    catOne[1] = new ScheduleEntry("Situps");
    catOne[2] = new ScheduleEntry("Squats");
    catOne[3] = new ScheduleEntry("Bench Work");
    catOne[4] = new ScheduleEntry("Partner Versions");
    catOne[5] = new ScheduleEntry("Running + Numbers");

    entries[CAT_ONE] = &catOne;
}

显然,ScheduleEntry是(和成员函数)在其他地方定义的,我在尝试编译时得到的错误如下:

gfi@testbox:~/test$ g++ -o test -std=c++0x main.cpp
In file included from main.cpp:6:
EntryList.h: In constructor ‘EntryList::EntryList()’:
EntryList.h:40: error: cannot convert ‘ScheduleEntry* (*)[6]’ to ‘ScheduleEntry**’ in assignment

在阅读了我能找到的锯齿状数组之后,这是我能想到的最简单,最易读的方法。第一件事 - 是吗?有更简单的方法吗?

接下来(当然)编译器错误。我怎么搞砸了打字?

提前致谢。 =)

修改

找到幸福的媒介。

#define CAT_ONE_COUNT    6
#define CAT_TWO_COUNT    7
#define CAT_THREE_COUNT 20
#define CAT_FOUR_COUNT   5
#define CAT_FIVE_COUNT   4
#define CAT_SIX_COUNT   20

enum {CAT_ONE, CAT_TWO, CAT_THREE, CAT_FOUR, CAT_FIVE};

class EntryList
{
    private:
        ScheduleEntry** entries[6];

public:
    EntryList();
    ~EntryList();

    std::string getEntry(int cat, int entry);
};

EntryList::EntryList()
{
    entries[CAT_ONE] = new ScheduleEntry*[CAT_ONE_COUNT];
    entries[CAT_ONE][0] = new ScheduleEntry("Pushup");
    entries[CAT_ONE][1] = new ScheduleEntry("Situps");
    entries[CAT_ONE][2] = new ScheduleEntry("Squats");
    entries[CAT_ONE][3] = new ScheduleEntry("Bench Work");
    entries[CAT_ONE][4] = new ScheduleEntry("Partner Versions");
    entries[CAT_ONE][5] = new ScheduleEntry("Running + Numbers");
}

1 个答案:

答案 0 :(得分:3)

语法错误是:

entries[CAT_ONE] = &catOne; // Arrays are secretly pointers so this won't work

替换为:

entries[CAT_ONE] = catOne; // catOne is a ScheduleEntry* [], which is a ScheduleEntry**

你可能会得到类似的东西:

const int width = 6;
int height[width] = { 6, 7, 20, 5, 4, 20 };

ScheduleEntry*** entries = new (ScheduleEntry**)[width];
for (int i = 0; i < width; ++i) entries[i] = new (ScheduleEntry*)[height[i]];

entries[0][0] = new ScheduleEntry("Pressups");
entries[0][1] = new ScheduleEntry("Situps");
entries[0][2] = new ScheduleEntry("Squats");
entries[0][3] = new ScheduleEntry("Bench Work");
entries[0][4] = new ScheduleEntry("Partner Versions");
entries[0][5] = new ScheduleEntry("Running + Numbers");
// etc etc