简体   繁体   English

C ++动态数组模板问题

[英]C++ Dynamic Array Template issue

I am a beginner programmer in school still, and I was assigned this problem: 我仍然是学校的初学者程序员,因此被分配了此问题:

"Make your own dynamic array template. It should allow creating contiguous arrays (filled with things of the same type) which you can extend without worrying about running out of space. “制作自己的动态数组模板。它应该允许创建连续的数组(填充相同类型的东西),您可以扩展这些数组而不必担心空间不足。

Do one version using malloc and free. 使用malloc和free做一个版本。

Do one version using new and delete." 使用new和delete做一个版本。”

So far this is what I have: 到目前为止,这就是我所拥有的:

#include <iostream>
#include <sstream>
#include "Array.h"
using namespace std;

int main(){

  Array<int> *testArray = new Array<int>(5);
  testArray->initArray();
  testArray->printArray();
  testArray->addData(7);
  testArray->printArray();
  return 0;
}

And here is the "Array.h" file: 这是“ Array.h”文件:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

template<typename T>
class Array{
  public:
  Array(int size){
    size = size;
    data = new T[size];

};
  Array<T> *addData(T dataToAdd){
    Array <T> *tmp = new Array <T> (this->size);
    tmp->data = this->data;
    Array <T> *newData = new Array<T> (this->size + 1);

    for (int i = 0; i < this->size + 1; ++i){
        if (i < this->size){
            //newData->data[i] = tmp->data[i];
            newData->setData(tmp->getData()[i], i);
        }
        else{
            //newData->data[i] = dataToAdd;
            newData->setData(dataToAdd, i);
        }
    }
    return newData;
};
  void initArray(){
    for (int i = 0; i < this->size; ++i){
        //this->data[i] = i;
        this->setData(i, i);
    }
};
  void printArray(){
    ostringstream oss;
    string answer = "";

    for (int i = 0; i < this->size; ++i){
        oss << this->data[i] + " ";
        //cout << this->data[i] << " ";
    }

    answer = oss.str();

    cout << answer << "asdf" << endl;
};
  T* getData(){
    return this->data;
}
  int getSize(){
    return this->size;
}
  void setData(T data, int index){
    this->getData()[index] = data;
}
private:
  int size;
  T* data;
};

So far what SHOULD happen in my main file is there should be an array of 5 ints, that are initialized to 0,1,2,3,4 from the initArray function. 到目前为止,应该在我的主文件中发生的是应该有一个由5个int组成的数组,这些数组已从initArray函数初始化为0、1、2、3、4。

Then it should print out the array, showing "0 1 2 3 4," add another "7" to it, then print the new array out showing "0 1 2 3 4 7." 然后,它应该打印出显示“ 0 1 2 3 4”的数组,并添加另一个“ 7”,然后打印出显示“ 0 1 2 3 4 7”的新数组。

For some reason, and I think it has something to do with losing data somehow when going between the two files, the field "data" of my Array class is not being properly changed. 出于某种原因,我认为这与在两个文件之间移动时丢失数据有关,我的Array类的“数据”字段未正确更改。

I even hardcoded a test for this in main where I wrote a for loop using the setData function that initializes the Array to "0 1 2 3 4," and then manually printed out these values with another for loop, but the output was only "0 0 0 0 0." 我什至在主程序上对此进行了硬编码,在其中我使用setData函数编写了一个for循环,该函数将Array初始化为“ 0 1 2 3 4”,然后使用另一个for循环手动打印出这些值,但输出仅为0 0 0 00。”

Right now, as the code is, the output is: 现在,如代码所示,输出为:

asdf
asdf

As it was outputting whitespace before so I added the "asdf"'s to see if my printArray worked at all. 由于之前是输出空格,因此我添加了“ asdf”,以查看我的printArray是否可以正常工作。

To sum up, why is the data in my private field "data" not being properly stored? 总结一下,为什么我的私有字段“数据”中的数据没有正确存储? I am very new to programming in c++ and any advice would be greatly appreciated. 我对使用C ++编程非常陌生,任何建议将不胜感激。 Thank you for your time, and if there is anything you do not understand please ask for clarification and I will do my best. 谢谢您的宝贵时间,如果有任何您不了解的内容,请澄清一下,我会尽力而为。

EDIT: problem solved! 编辑:问题解决了! Thank you everyone who helped, the issue was with my constructor and how I was calling my functions in main. 谢谢大家的帮助,问题出在我的构造函数以及我如何在main中调用函数。

One issue is your constructor: 一个问题是您的构造函数:

 Array(int size){
   size = size;
   data = new T[size];
 };

The way you have it, you're just assigning your size argument to itself, which has no effect. 拥有它的方式只是将您的size参数分配给它自己,这没有任何效果。 One way to fix it would be to use a different name for the argument: 解决该问题的一种方法是对参数使用不同的名称:

 Array(int size_arg){
   size = size_arg;
   data = new T[size_arg];
 };

However, the preferred way is to use the constructor initializer syntax: 但是,首选方法是使用构造函数初始化器语法:

Array(int size) : size(size), data(new T[size]) {};

With the constructor initializer syntax, the compiler knows that you are trying to initialize specific members and doesn't get confused between the argument name and the member name. 使用构造函数初始化器语法,编译器知道您正在尝试初始化特定成员,并且不会在参数名称和成员名称之间造成混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM