[英]dynamic string array constructor and null returns
class DSArray {
private:
int size;
string *dynamicArray;
public:
DSArray();
DSArray(int size, string []);
~DSArray();
int getSize() const;
void addEntry(string newString);
bool deleteEntry(string find);
string getEntry(const int index) const;
void operator = (const DSArray& obj);
string operator [] (int index);
friend ostream& operator << (ostream &out, const DSArray& array);
};
DSArray::DSArray(){
size = 0;
dynamicArray = NULL;
}
DSArray::DSArray(int size, string []){
dynamicArray = new string[size];
for (int i = 0; i < size; i++){
dynamicArray[i] = getEntry(i);
}
size = getSize();
}
DSArray::~DSArray(){
delete[] dynamicArray;
}
int DSArray::getSize() const{
return size;
}
void DSArray::addEntry(string newString){
string *tempPtr = dynamicArray;
dynamicArray = new string[size + 1];
dynamicArray[size].assign(newString);
for(int i = 0; i < size; i++){
dynamicArray[i] = tempPtr[i];
}
if(size > 0){
delete[] tempPtr;
}
size++;
}
ostream& operator << (ostream &out, const DSArray& array){
for (int i = 0; i < array.getSize(); i++)
out << array.dynamicArray[i];
return out;
}
bool DSArray::deleteEntry(string toDelete)
{
int index;
for(index = 0; index < size; index++)
{
if(dynamicArray[index] == toDelete) break;
}
if(index == size) return false;
string *newArray = new string[size--];
int i;
for(i = 0; i < index; i++) newArray[i] = dynamicArray[i];
for(int k = index + 1; k <= size; k++, i++) newArray[i] = dynamicArray[k];
delete [] dynamicArray;
dynamicArray = newArray;
return true;
}
string DSArray::getEntry(const int index) const{
static string emptyStr = "";
if(index > size){
return emptyStr;
}
else{
return dynamicArray[index];
}
}
void DSArray::operator = (const DSArray& obj){
DSArray temp(obj);
if(this->size != 0)
delete[] this->dynamicArray;
this->size = obj.getSize();
this->dynamicArray = new string [this->size];
for(int i = 0; i < this->size; i++)
this->dynamicArray[i] = obj.getEntry(i);
}
string DSArray::operator [] (int index){
string emptyString = "";
if (index >= size){
return emptyString;
}
return dynamicArray[index];
}
int main()
{
DSArray foods;
string x[] = {"Burrito", "Sushi", "Pizza"};
DSArray A(3, x);
cout << A;
A[3] = "Spaghetti";
foods.addEntry("Steak");
foods.deleteEntry("Sushi");
return 0;
}
I'm trying to test my functions in main but it's not actually displaying anything in the console. 我正在尝试在main中测试我的函数,但它实际上并没有在控制台中显示任何内容。 I was told that my constructor isn't setting the size correctly because calling getSize returns the object's size which hasn't been initialized yet.
有人告诉我,我的构造函数没有正确设置大小,因为调用getSize会返回尚未初始化的对象大小。 How exactly do i fix this?
我究竟如何解决这个问题? Also does anyone know if the getEntry function get return a null instead of an empty string?
也有人知道getEntry函数是否返回null而不是空字符串? My assignment says it should return a null, however, when i do return a null it gives me a runtime error.
我的任务说它应该返回一个null,但是,当我确实返回一个null时,它会给我一个运行时错误。 Any help is appreciated.
任何帮助表示赞赏。 Thanks!
谢谢!
You are not initializing the member variable size
in the constructor. 您没有在构造函数中初始化成员变量
size
。 Use: 采用:
DSArray::DSArray(int size, string input[]) : size(size) { ... }
Also, the line 还有,行
dynamicArray[i] = getEntry(i);
does not do anything useful. 没有做任何有用的事情。 It is essentially self assignment.
它本质上是自我分配。
You can avoid hard to follow code by using a different name for input argument. 您可以通过为输入参数使用不同的名称来避免难以遵循代码。 Also, copy the array of input strings to the member variable.
此外,将输入字符串数组复制到成员变量。
DSArray::DSArray(int sizeIn, string input[]) : size(sizeIn) {
dynamicArray = new string[size];
for (int i = 0; i < size; i++){
dynamicArray[i] = input[i];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.