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