2

在我的新工作中,我意识到迄今为止我实际使用的 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");
}
4

1 回答 1

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
于 2012-06-18T22:40:01.293 回答