简体   繁体   中英

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.

Do one version using new and 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:

#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.

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."

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.

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."

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.

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. 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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