动态阵列输出的问题
我正在使用用于动态数组的模板类,并且我的代码已经成功编译,但是在动态数组中,我对给定元素的获取函数似乎无法正常工作。每当我输出功能时,似乎都没有输出,但是这没有意义,因为我的大小功能正常,并且表明数组中确实有东西,我只是无法输出它。谁能帮忙?
#include <iostream>
using namespace std;
template <class T>
class Wrapper{
private:
T* array;
int size;
public:
Wrapper(){
array = NULL;
size = 0;
}
int returnSize(){
return size;
}
void addEntry(string input){
if(returnSize()==0){
array = new string[returnSize()+1];
array[returnSize()] = input;
size+=1;
}
else{
T* newArray = new T[returnSize()+1];
for(int i = 0; i<returnSize(); i++){
newArray[i] = array[i];
}
newArray[returnSize()] = input;
size+=1;
string *array = new string[returnSize()];
array = newArray;
}
}
bool deleteEntry(string input){
int mySize = returnSize();
string* testArray = new string[mySize];
for(int i = 0; i<mySize; i++){
if(array[i]==input){
return true;
break;
}
}
string*newArray = new string[mySize-1];
for(int i = 0; i<mySize; i++){
if(array[i]!=input){
newArray[i+=1] = array[i];
}
}
delete[]array;
array = newArray;
return true;
}
string getEntry(int input){
if(input>size){
return NULL;
}
return this->array[input];
}
void operator=(const Wrapper w){
this->size = w.returnSize();
string *newArray = new string[size];
for(int i = 0; i<size; i++){
newArray[i] = w.getEntry(i);
}
this->array = newArray;
}
~Wrapper(){
delete[] array;
}
};
int main(){
Wrapper<string> w;
w.addEntry("sam");
w.addEntry("Josh");
w.addEntry("tom");
cout<<"Here"<<endl;
cout<<w.returnSize();
for(int i = 0; i<<w.returnSize(); i++){
cout<<w.getEntry(i);
}
}
I'm working with a template class for dynamic arrays, and my code has compiled successfully, however my get function for a given element in the dynamic array does not seem to be working. Nothing seems to be outputted whenever I output the function, but this doesn't make sense, because my size function works fine, and shows that there is indeed something within the array, I just can't output it. Can anyone help?
#include <iostream>
using namespace std;
template <class T>
class Wrapper{
private:
T* array;
int size;
public:
Wrapper(){
array = NULL;
size = 0;
}
int returnSize(){
return size;
}
void addEntry(string input){
if(returnSize()==0){
array = new string[returnSize()+1];
array[returnSize()] = input;
size+=1;
}
else{
T* newArray = new T[returnSize()+1];
for(int i = 0; i<returnSize(); i++){
newArray[i] = array[i];
}
newArray[returnSize()] = input;
size+=1;
string *array = new string[returnSize()];
array = newArray;
}
}
bool deleteEntry(string input){
int mySize = returnSize();
string* testArray = new string[mySize];
for(int i = 0; i<mySize; i++){
if(array[i]==input){
return true;
break;
}
}
string*newArray = new string[mySize-1];
for(int i = 0; i<mySize; i++){
if(array[i]!=input){
newArray[i+=1] = array[i];
}
}
delete[]array;
array = newArray;
return true;
}
string getEntry(int input){
if(input>size){
return NULL;
}
return this->array[input];
}
void operator=(const Wrapper w){
this->size = w.returnSize();
string *newArray = new string[size];
for(int i = 0; i<size; i++){
newArray[i] = w.getEntry(i);
}
this->array = newArray;
}
~Wrapper(){
delete[] array;
}
};
int main(){
Wrapper<string> w;
w.addEntry("sam");
w.addEntry("Josh");
w.addEntry("tom");
cout<<"Here"<<endl;
cout<<w.returnSize();
for(int i = 0; i<<w.returnSize(); i++){
cout<<w.getEntry(i);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是为了收集调查结果,如果您从
Addentry
删除此行,然后将循环的最终结果更改为:
这是您所期望的。我自己运行。
Just to collect the findings, if you delete this line from
addEntry
:and change your final for loop to:
then this does exactly what you expect. I've run it myself.