-3

So, I'm having this problem. Basically I made a class that contains a string, then a function that makes an array of that class that holds the string and I want to return it to main() but just return wordBank[]; doesn't work. Can someone explain to me why my code doesn't work and what I need to do for it to work? Sorry I'm a novice in C++. Thank you, here's the code:

#include <iostream>
#include <fstream>

using namespace std;

// wordlist object
class wordList {
public:
    string word;
};

// function that is supposed to fill my wordList class object with words
wordList* readWordList() {
    wordList wordBank[3];
    string wlist = "wordlist.txt";
    ifstream data(wlist);
    while (!data.eof()) {
        for (int i = 0;i < 3;i++) {
            data >> wordBank[i].word;
        }
    }
    data.close();
    return wordBank;
}

//main function
int main()
{
    wordList wordBank[2];
    wordBank = *readWordList() ; // ?
    std::cout << wordBank[2].word;
}
pazuzu
  • 1
  • 1

3 Answers3

4

The easiest solution is to use std::vector<wordList>. Arrays are a building block - simple but not very convenient.

MSalters
  • 173,980
  • 10
  • 155
  • 350
1

wordBank is local to the readWordList() function. So the second that function ends, wordBank and all the memory it has no longer belong to you. In other words, you're invoking undefined behavior by trying to look at what was returned there.

Instead, I'd advise that you simply use a vector:

std::vector<wordList> readWordList() {
    std::vector<wordList> wordBank(3);
    string wlist = "wordlist.txt";
    ifstream data(wlist);
    while (!data.eof()) {
        for (int i = 0;i < 3;i++) {
            data >> wordBank[i].word;
        }
    }
    data.close();
    return wordBank;
}
scohe001
  • 15,110
  • 2
  • 31
  • 51
  • I am curious what is wrong about this answer, really no clue – 463035818_is_not_an_ai Jul 10 '20 at 13:20
  • 1
    I have no idea why we permanently answer duplicates... – Klaus Jul 10 '20 at 13:21
  • lol. I am too, especially since my similar answer got the same treatment :-) – Jeffrey Jul 10 '20 at 13:21
  • 1
    Seems likely someone downvoted all the answers because this is a frequent duplicate question. – François Andrieux Jul 10 '20 at 13:25
  • @Klaus because the same question being asked over and over again, because the mechanism that is in place to flag as dupe is rather poor, and because answering is still the easiest, just some thoughts... – 463035818_is_not_an_ai Jul 10 '20 at 13:26
  • 2
    @Klaus the duplicated answer selected suggests returning array as pointer. It is severely outdated and most would rather suggest std::vector in this day and age – Jeffrey Jul 10 '20 at 13:26
  • 1
    @Jeffrey: There are lot more duplicates to this question and also better ones. I agree that there is a need of sorting such duplicate duplciates, but I do not agree that answering again is a better solution. – Klaus Jul 10 '20 at 13:28
  • 1
    @Jeff in all fairness, the StackOverflow format dictates it would've been better to post this on the dupe target itself to keep it up to date rather than on this dupe that'll get lost forever. The issue here was that the question wasn't flagged as dupe by the time I answered, so I just didn't know. – scohe001 Jul 10 '20 at 13:29
  • 1
    "The issue here was that the question wasn't flagged as dupe by the time I answered, so I just didn't know." That is exactly the problem I see! It takes a bit of effort to find a dupe and it is "easier" to create another duplicate. And "earning" votes for duplicates is pushing that problem also. These discussions are already on meta, but no solution... – Klaus Jul 10 '20 at 13:32
  • Maybe I should start following meta. I could start pushing for my ELO-rating-based reputation system that addresses all of these. – Jeffrey Jul 10 '20 at 13:33
1

You cannot return an array. You could return a pointer (to the beginning of the array) but then you'll get into issues as the lifetime of the array is only the function scope.

The proper way is to use a std::vector instead.

std::vector<wordList> wordBank(3);

It behaves like an array an can be returned by value.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42