我有一个标题和.cpp文件。我在初始化阵列时遇到困难。问题是我无法指定数组大小。大小取决于用户输入的元素数量。当我创建一个新的dynamicArray时,我相信我必须使用" new"像dynamicArray = new string [sizeof(array)](因为我必须根据赋值删除内存)但是当我通过Linux运行它时,它表示它不能出现在常量表达式中。
我对C ++还有点不熟悉。任何反馈都表示赞赏。
错误是什么样的:
我还遇到了标题重新定义的问题,我认为#pragma曾经帮助过。 我知道#include" stdafx.h"对Linux有害。我每次在linux上运行它都会删除它。
DynamaicStringArray.cpp
// DynamicStringArray.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "DynamicStringArray.h"
#include "Assignment9Test_V1.cpp"
// copy constructor that copies the array
DynamicStringArray::DynamicStringArray(string array[]) {
// string *dynamicArray = new string[sizeof(array)]; // without pointer, error with conversion between types
dynamicArray = new string[sizeof(array)];
for (int i = 0; i < sizeof(array); i++) {
dynamicArray[i] = array[i];
}
}
// default constructor
DynamicStringArray::DynamicStringArray() {
size = 0;
dynamicArray = NULL;
// dynamicArray = new string[size];
}
// frees up dynamic array memory
void DynamicStringArray::destructor(string array[]) {
delete[] array;
array = NULL;
}
// return number of entries in array
int DynamicStringArray::getSize() {
return size;
}
// creates a new dynamic array one element larger than dynamicArray
// copies all elements of dynamicArray to new array and
// add new string to end of new array
// increment size
// delete old dynamicArray
// set new array as dynamicArray
void DynamicStringArray::addEntry(string newString) {
string *newArray;
newArray = new string[size + 1];
for (int i = 0; i < size; i++) {
newArray[i] == dynamicArray[i];
}
int endIndex = sizeof(newArray);
newArray[endIndex] = newString;
destructor(dynamicArray);
dynamicArray = newArray;
}
// searches for dynamicArray for specific string,
// if string not found, return false
// if string is found, create new one size smaller dynamic array
// than dynamicArray
// copy elements of old dynamicArray to new array without the string
// delete old dynamicArray
// decrement size
// return true
bool DynamicStringArray::deleteEntry(string deleteMe) {
string *newArray;
newArray = new string[size - 1];
bool isFound = false;
for (int i = 0; i < size; i++) {
if (dynamicArray[i] == deleteMe) {
i++;
isFound = true;
}
else if (dynamicArray[i] != deleteMe) {
newArray[i] == dynamicArray[i];
}
}
if (isFound) {
return true;
destructor(dynamicArray);
dynamicArray = newArray;
}
else if (!isFound) {
return false;
}
}
// returns the string at that index
string DynamicStringArray::getEntry(int findMe) {
return dynamicArray[findMe];
}
DynamicStringArray.h
#pragma once
#include <string>
#include <iostream>
using namespace std;
class DynamicStringArray {
public:
DynamicStringArray();
DynamicStringArray(string array[]);
// overloading the assignment operator
void destructor(string array[]);
int getSize();
void addEntry(string);
bool deleteEntry(string);
string getEntry(int);
private:
int size; // holds number of entries in the array
string *dynamicArray; // references a dynamic array of type string
};
答案 0 :(得分:0)
您使用new
(特别是new[]
和delete[]
)解决此问题的直觉是正确的。您必须在运行时动态分配数组,因为您在编译时不知道元素的数量。但是,您没有正确使用new[]
/ delete[]
,这就是您遇到错误的原因(代码中的许多其他问题)。
尝试更像这样的东西:
DynamicStringArray.h
#pragma once
#include <string>
class DynamicStringArray {
public:
DynamicStringArray();
DynamicStringArray(const std::string *array, int arraySize);
DynamicStringArray(const DynamicStringArray &array);
~DynamicStringArray();
DynamicStringArray& operator=(const DynamicStringArray &array);
int getSize() const;
void addEntry(const std::string &newString);
bool deleteEntry(const std::string &deleteMe);
std::string getEntry(int findMe) const;
private:
int size; // holds number of entries in the array
string *dynamicArray; // references a dynamic array of type string
};
DynamicStringArray.cpp
// DynamicStringArray.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "DynamicStringArray.h"
// default constructor
DynamicStringArray::DynamicStringArray() {
size = 0;
dynamicArray = NULL;
}
// constructor that copies an array
DynamicStringArray::DynamicStringArray(const std::string *array, int arraySize) {
size = arraySize;
dynamicArray = new std::string[size];
for (int i = 0; i < size; i++) {
dynamicArray[i] = array[i];
}
}
// copy constructor that copies the array
DynamicStringArray::DynamicStringArray(const DynamicStringArray &array) {
size = array.size;
dynamicArray = new std::string[size];
for (int i = 0; i < size; i++) {
dynamicArray[i] = array.dynamicArray[i];
}
}
// frees up dynamic array memory
DynamicStringArray::~DynamicStringArray() {
delete[] array;
}
// copy assignment operator that copies the array
DynamicStringArray& DynamicStringArray::operator=(const DynamicStringArray &array) {
if (&array != this) {
DynamicStringArray copied(array);
std::string *temp = copied.dynamicArray;
copied.dynamicArray = dynamicArray;
dynamicArray = temp;
size = array.size;
}
return *this;
}
// return number of entries in array
int DynamicStringArray::getSize() const {
return size;
}
// creates a new dynamic array one element larger than dynamicArray
// copies all elements of dynamicArray to new array and
// add new string to end of new array
// increment size
// delete old dynamicArray
// set new array as dynamicArray
void DynamicStringArray::addEntry(const std::string &newString) {
std::string *newArray = new std::string[size + 1];
for (int i = 0; i < size; i++) {
newArray[i] = dynamicArray[i];
}
newArray[size] = newString;
delete[] dynamicArray;
dynamicArray = newArray;
++size;
}
// searches the dynamic array for a specific string,
// if string not found, return false
// if string is found, create new array one size smaller than dynamicArray
// copy elements of old dynamicArray to new array without the string
// delete old dynamicArray and replace with new array
// decrement size
// return true
bool DynamicStringArray::deleteEntry(const string &deleteMe) {
for (int i = 0; i < size; i++) {
if (dynamicArray[i] == deleteMe) {
std::string *newArray = new std::string[size - 1];
for (int j = 0; j < i; ++j) {
newArray[j] = dynamicArray[j];
}
for (int j = i+1; j < size; ++j) {
newArray[j-1] = dynamicArray[j];
}
delete[] dynamicArray;
dynamicArray = newArray;
--size;
return true;
}
}
return false;
}
// returns the string at that index
std::string DynamicStringArray::getEntry(int findMe) const {
return dynamicArray[findMe];
}
话虽如此,使用std::vector
是首选解决方案。它是一个动态大小的数组,为您包装new[]
/ delete[]
。使用它比直接使用new[]
/ delete[]
更简单,更安全,例如:
DynamicStringArray.h
#pragma once
#include <string>
#include <vector>
class DynamicStringArray {
public:
DynamicStringArray();
DynamicStringArray(const std::string *array, int arraySize);
int getSize() const;
void addEntry(const std::string &newString);
bool deleteEntry(const std::string &deleteMe);
std::string getEntry(int findMe) const;
private:
std::vector<std::string> dynamicArray; // dynamic array of type string
};
DynamicStringArray.cpp
// DynamicStringArray.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "DynamicStringArray.h"
#include <algorithm>
// default constructor
DynamicStringArray::DynamicStringArray() {
}
// copy constructor that copies the array
DynamicStringArray::DynamicStringArray(const std::string *array, int arraySize) :
dynamicArray(array, array + arraySize) {
}
// return number of entries in array
int DynamicStringArray::getSize() const {
return dynamicArray.size();
}
// appends a new element to the end of the dynamic array
void DynamicStringArray::addEntry(const std::string &newString) {
dynamicArray.push_back(newString);
}
// searches the dynamic array for a specific string,
// if string not found, return false
// if string is found, remove it and return true
bool DynamicStringArray::deleteEntry(const std::string &deleteMe) {
std::vector<std::string>::iterator iter = std::find(dynamicArray.begin(), dynamicArray.end(), deleteMe);
if (iter != dynamicArray.end())
{
dynamicArray.erase(iter);
return true;
}
return false;
}
// returns the string at that index
std::string DynamicStringArray::getEntry(int findMe) const {
return dynamicArray[findMe];
}