1

I am trying to create an array in my UnsortedList class. I specified to create an array in the header file, and I also specified the MAX_SIZE, which is equal to 10. However, whenever I create my object of the class, the default constructor does not create that array with the MAX_SIZE. I am unsure what I am doing wrong. I also get an error saying "stack around the variable 'myList' was corrupted". Also, just as a side note, can I initialize the array values when the default constructor is called, instead of creating a function to do it?

"UnsortedList.h" header file:

#pragma once

class UnsortedList {
public:
    UnsortedList();
    bool IsFull(); //Determines whether the list is full or not (returns T or F)
    int GetLength(); //Gets the length of the list
    void SetListValues();
private:
    int length;
    const int MAX_ITEMS = 10;
    int numbers[];
};

"UnsortedList.cpp" file:

#pragma once
#include "UnsortedList.h"
#include <fstream>
#include <iostream>
using namespace std;

UnsortedList::UnsortedList() {
    length = 0; //sets length to 0
    numbers[MAX_ITEMS]; //sets array maximum size to MAX_ITEMS (10 as indicated in UnsortedList.h)
}

bool UnsortedList::IsFull() {
    return (length == MAX_ITEMS);
}

int UnsortedList::GetLength() {
    return length;
}

void UnsortedList::SetListValues() {
    ifstream inFile;
    inFile.open("values.txt");

    int x = 0;
    while (!inFile.eof()) {
        inFile >> numbers[x];
        x++;
    }
}

"main.cpp" file:

#include <iostream>
#include <string>
#include "UnsortedList.h"
using namespace std;

int main() {

    UnsortedList myList;
    myList.SetListValues();

    return 0;
}
  • 3
    `numbers[MAX_ITEMS]; ` does not create or resize the array. – drescherjm Sep 08 '20 at 21:40
  • 3
    `numbers[MAX_ITEMS];` doesn't do what you think it does. At best, nothing. At worst, segmentation fault. And did you hear about `std::vector` or `std::array`? – ypnos Sep 08 '20 at 21:40
  • 1
    `while (!inFile.eof()) {` avoid this pattern because of this: [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm Sep 08 '20 at 21:43
  • You want [`std::array`](https://en.cppreference.com/w/cpp/container/array) in c++ – πάντα ῥεῖ Sep 08 '20 at 21:44
  • 1
    Also `while (!inFile.eof())` is [wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – πάντα ῥεῖ Sep 08 '20 at 21:47
  • @ypnos no, I haven't heard of either of those. Should I be using them? –  Sep 08 '20 at 21:47
  • @AG77 _" Should I be using them?"_ Definitely. – πάντα ῥεῖ Sep 08 '20 at 21:47

1 Answers1

2

I recommend you use std::array or std::vector, but if you must use C arrays, then your definition in the header needs correcting:

class UnsortedList {
// ...
    const static int MAX_ITEMS = 10;
    int numbers[MAX_ITEMS];
};

You can remove the corresponding line in the constructor. The file reading method also needs correcting:

void UnsortedList::SetListValues() {
    ifstream inFile;
    inFile.open("values.txt");

    int x = 0;
    int read_value;

    // x < MAX_ITEMS to avoid out of bounds access
    while (x != MAX_ITEMS && inFile >> read_value) 
    {
        numbers[x++] = read_value;

        length++; // I assume you also want to increment the length at this point?
    }
}


Edit: As noted by @πάνταῥεῖ, there is no good reason to use C style arrays when the standard provides std::array. Not much changes, it is declared as:

std::array<int, MAX_ITEMS> numbers;

You can use operator[] as with the C array. This is preferable as it provides a richer API and can be used like other C++ containers, i.e. with STL algorithms.

Mansoor
  • 2,357
  • 1
  • 17
  • 27
  • In the header defintion, I tried doing ```int numbers[MAX_ITEMS];``` but get an error saying "a nonstatic member reference must be relative to a specific object –  Sep 08 '20 at 21:53
  • @AG77 I forgot the static keyword before the size member! – Mansoor Sep 08 '20 at 21:56
  • @Mansoor _"but if you must use C arrays"_ There's no reason. If you need to interact with plain c APIs there's [`std::array::data()`](https://en.cppreference.com/w/cpp/container/array/data) at hand. Would be a great improvement for your answer to mention that. – πάντα ῥεῖ Sep 08 '20 at 22:03
  • @πάνταῥεῖ artificially imposed requirement, like an academic exercise? I agree with your point. The answer would look similar with `std::array`. – Mansoor Sep 08 '20 at 22:06
  • @Mansoor _"artificially imposed requirement like an academic exercise?"_ Well, in good courses they should teach how to write less error prone and production ready code. But teachers (especially the older ones) are lazy, and slacking to update their course matierials. - Just my 2¢ – πάντα ῥεῖ Sep 08 '20 at 22:11