【发布时间】:2014-10-12 22:11:03
【问题描述】:
我很难处理不断弹出的错误。这是一个家庭作业,其中很大一部分包含在一个单独的 .h 文件中,所以我不会发布所有代码以保持完整。以下是相关部分:
//在.h中:
class array_list
{
private:
unsigned int * m_storage;
// points to current position in the list
// in documentation # marks the current position in the list
// ie. if l={#1,2,3} the 1 is the current position in the list
// if l={1,2,#3} the 3 is the current position in the list
// the # is ONLY for documentation purposes
unsigned int m_current;
unsigned int m_size;
unsigned int m_capacity;
// 等等
//指南:
// Construct a new, empty list.
//
// Pre-conditions:
// none
// Post-conditions:
// m_storage is initialized to point to an array of size INIT_SIZE
// m_capacity is initialized to INIT_SIZE
// m_current is set to -1
// m_size is set to 0
//我写的:
array_list::array_list()
{
int arrayOf[INIT_SIZE];
m_storage = arrayOf[]; /* <---THE PROBLEM LINE */
m_capacity = INIT_SIZE;
m_current = -1;
m_size = 0;
}
由于某种原因,我得到一个错误,即编译器在指示的行上的 ']' 标记之前需要一个主表达式。我已经阅读了我的笔记并进行了一些谷歌搜索,看起来这确实是声明一个数组并使用预定义指针指向它的方式,不是吗?谁能帮我解决这个问题?谢谢。
【问题讨论】:
-
m_storage = (unsigned int*)arrayOf。为什么你觉得你需要这对空括号?这不是有效的语法。并使arrayOf成为unsigned int的数组(或者m_storage和int*),这样就不需要演员表了。哦,当构造函数返回时,arrayOf将被销毁,留下m_storage一个悬空指针。 -
你为什么要创建一个本地数组?你不应该只是
m_storage = new unsigned int [INIT_SIZE];