在评论中回答的问题 由于我的声誉,我无法以常规方式回答。稍后我将在答案中添加详细信息,已在评论中解决。谢谢。* *
大家好 -
毫无疑问,您会根据这个问题看到,我是 C++ 新手,但有一些高级语言的经验。(这似乎比帮助更痛苦)
对于一个类,我需要为输入整数的数组创建一个包装器。(课程的这个阶段没有模板)我还需要让课程有一个非零的起始索引。我在类中使用一个成员数组来存储我的数据(此时类中还没有向量)并从公共方法进行一些转换以访问适当的内部数组元素。
我遇到的问题是我在编译时不知道内部数组的大小,所以我将它声明为类全局指针并在构造函数中设置大小。问题区域中的代码片段如下:
int *list;
safeArray::safeArray(int start, int initialSize)
{
if(initialSize <= 0)
{
throw "Array size must be a positive integer";
}
maxSize = initialSize + 1;
startIndex = start;
endIndex = start + initialSize;
list = new int[maxSize]; // Error thrown here
int *tempArray = new int[maxSize];
copyArray(tempArray);
clearArray();
}
我得到的错误是
Incompatible types in assignment of 'int*' to 'int[0u]'
我不是 100% 确定 int[0u] 的类型是什么。那是文字值零并且 u 是无符号的吗?我已经在调试器中检查了 maxSize 保存一个值,并且我还用一个常量整数值替换了它并得到了同样的错误。
因为我的int *tempArray = new int[maxSize];
线路有效,我认为这可能与需要同时声明和调整大小有关,所以我选择做一个 memcpy。(这实际上超出了分配的范围,所以我肯定还缺少其他东西) memcpy 失败,因为我似乎正在破坏我的其他变量。当我在 GDB 中打印列表的地址时,它为我提供了与代码中另一个全局变量相同的地址,因此该路由似乎也超出了赋值的范围。
我在其他论坛上看到的共同主题是您不能像其他变量一样分配数组,但我认为这不会包括该new
声明。我的假设错了吗?
我目前看到的唯一编译错误是上面提到的那个,我list = new int[maxSize];
在代码中的每个语句中都看到了它。
我的问题是:
什么是 int[0u] 类型,该类型在哪里生成?它必须来自新的声明,对吗?
在类中利用动态数组资源的最佳方式是什么?除了使用向量?=)
我认为这就是所有相关信息,但如果我错过了关键数据,我深表歉意。下面是其余的实现代码。
/*
* safeArray.cpp
* safearray
*
* Created by Jeffery Smith on 6/1/11.
*
*
*/
#include "safeArray.h"
#include <iostream>
using namespace std;
int startIndex = 0;
int endIndex = 0;
int maxSize = 1;
int currentSize = 0;
int *list;
safeArray::safeArray(int start, int initialSize)
{
if(initialSize <= 0)
{
throw "Array size must be a positive integer";
}
maxSize = initialSize + 1;
startIndex = start;
endIndex = start + initialSize;
list = new int[maxSize]; // Error thrown here
int *tempArray = new int[initialSize + 1];
copyArray(tempArray);
clearArray();
}
safeArray::safeArray(const safeArray &sArray)
{
list = new int[sArray.maxSize];
copyArray(sArray);
startIndex = sArray.startIndex;
endIndex = sArray.endIndex;
maxSize = sArray.maxSize;
currentSize = sArray.currentSize;
}
void safeArray::operator=(const safeArray &right)
{
list = new int[right.maxSize];
copyArray(right);
startIndex = right.startIndex;
endIndex = right.endIndex;
maxSize = right.maxSize;
currentSize = right.currentSize;
}
safeArray::~safeArray()
{
delete [] list;
}
int safeArray::operator[](int index)
{
if(OutofBounds(index))
{
throw "You tried to access an element that is out of bounds";
}
return list[index - startIndex];
}
void safeArray::add(int value)
{
if(this->isFull())
{
throw "Could not add element. The Array is full";
}
currentSize++;
list[currentSize + startIndex];
}
void safeArray::removeAt(int value)
{
if(OutofBounds(value))
{
throw "The requested element is not valid in this list";
}
compressList(value);
currentSize--;
}
void safeArray::insertAt(int location, int value)
{
if(OutofBounds(location) || this->isFull())
{
throw "The requested value is either out of bounds or the list is full";
}
expandList(location, value);
currentSize++;
}
void safeArray::clearList()
{
clearArray();
}
bool safeArray::isFull()
{
return(maxSize == currentSize);
}
int safeArray::length()
{
return currentSize;
}
int safeArray::maxLength()
{
return this->maxSize;
}
bool safeArray::isEmpty()
{
return(currentSize == 0);
}
bool safeArray::OutofBounds(int value)
{
return (value > endIndex || value < startIndex);
}
void safeArray::clearArray()
{
for(int i = 0; i < maxSize; i++)
{
list[i] = 0;
}
currentSize = 0;
}
void safeArray::compressList(int value)
{
for(int i = value; i < endIndex; i++)
{
list[i] = list[i + 1];
}
}
void safeArray::expandList(int location, int value)
{
int tempHolder = list[location];
list[location] = value;
for(int i = location; i < endIndex; i++)
{
tempHolder = list[location];
list[location] = value;
value = tempHolder;
}
}
void safeArray::copyArray(int *srcAddr )
{
memcpy(list, srcAddr, sizeof(int) * maxSize);
}
void safeArray::copyArray(const safeArray &sArray)
{
memcpy(list, &sArray, sizeof(int) * maxSize);
}
这是标题定义:
/*
* safeArray.h
* safearray
*
* Created by Jeffery Smith on 6/1/11.
* Copyright 2011 Accenture. All rights reserved.
*
*/
class safeArray {
public:
safeArray(int,int); //Standard constructor
~safeArray(); //Destructor
int operator[](int);
void operator=(const safeArray&); //Assignment overload
safeArray(const safeArray &sArray); //Copy Constructor
void add(int);
int maxLength();
int length();
bool isFull();
bool isEmpty();
void clearList();
void removeAt(int);
void insertAt(int,int);
protected:
int list[];
int startIndex;
int endIndex;
int maxSize;
int currentSize;
private:
void clearArray();
bool OutofBounds(int);
void expandList(int,int);
void compressList(int);
void copyArray(int*);
void copyArray(const safeArray&);
};