0

I'm challenging myself to create my own custom integer array by making a class and structure with some functions. When I run the program I don't get an error, but the data of the index isn't being set, and I don't understand why.

Array class file

#include "IntArray.hpp"
using namespace std;

struct IntArrayIndex {
public:
    IntArrayIndex* next = NULL;
    int data;
    
    IntArrayIndex(int Data) {
        this->data = Data;
    }
    IntArrayIndex() {
        this->data = 0;
    }
};

class IntArray {
public:
    IntArrayIndex head = *(new IntArrayIndex());
    int length;
    
    IntArray() {
        this->length = 1;
        this->head = *(new IntArrayIndex());
    }

    int getIndex(int index) {
        if (index >= length) {
            throw invalid_argument("Index out of range. Returned -1");
        }
        
        IntArrayIndex current = head;
        if (index > 0) {
            current = *current.next;
            getIndex(--index);
        }
        return current.data;
    }
    
    void setIndex(int index, int data) {
        IntArrayIndex current = head;
        if (index > 0) {
            if (current.next == NULL) {
                current.next = new IntArrayIndex();
                length++;
            }
            current = *current.next;
            setIndex(--index, data);
        }
        current.data = data;
    }
};

Main file

#include <iostream>
#include "IntArray.cpp"

int main(int argc, const char * argv[]) {
    IntArray* l = new IntArray();
    l->setIndex(0, 1);
    std::cout << l->getIndex(0);
}

OUTPUT: 0 Program ended with exit code: 0

  • 1
    *I don't get an error* just means that the syntax is correct. It does not mean that the logic is right, though. You need to use a debugger to step through the code to figure out where the problem is located. If you don't know how to use a debugger, now is the perfect time to learn - it's the most powerful tool a programmer has to solve this sort of issue. – Ken White Oct 15 '21 at 04:48
  • Looks like you want to create a linked list with `setindex/getindex` method. First create the linked list, then proceed to next step. You have to change `IntArrayIndex head = *(new IntArrayIndex());` to `IntArrayIndex *head;`, then put `head = new IntArrayIndex();` in the constructor ... – Barmak Shemirani Oct 15 '21 at 05:05
  • 1
    Oh wow, I've actually never used that, I was able to find the fault with the logic by using it, thanks! I needed to create a pointer to the head index as I wasn't actually changing the value of the address. – Alex Niadore Oct 15 '21 at 05:11
  • 1
    Are you coming from Java or similar background? Things like `*(new...)` are typical for Java programmers making their first steps in C++. Java experience does not transfer to C++ well. I recommend getting a good C++ textbook and working through exercises. – n. m. could be an AI Oct 15 '21 at 05:12
  • @BarmakShemirani Thank you, I got it to work by changing some pointers and addresses. – Alex Niadore Oct 15 '21 at 05:14
  • @n.1.8e9-where's-my-sharem. I'm learning java through school and taking c++ on the side; I'm mostly just inexperienced. I just heard about pointers, and I'm still in the learning progress. I was thinking of getting a book instead of watching youtube videos. Do you have any recommendations? – Alex Niadore Oct 15 '21 at 05:17
  • [Here is the recommended book list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – n. m. could be an AI Oct 15 '21 at 05:20

1 Answers1

0

Changing the pointers in setIndex ended up fixing the problem:

void setIndex(int index, int data) {
        IntArrayIndex* current = &head;
        if (index > 0) {
            if (current->next == NULL) {
                current->next = new IntArrayIndex();
                length++;
            }
            current = current->next;
            setIndex(--index, data);
        }
        current->data = data;
    }