-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;
}
4
  • Is it acceptable for you to use std::vector? Commented Jul 10, 2020 at 13:19
  • use std::vector. Turn on warnings (the compiler is probably telling you why you can't do that) Commented Jul 10, 2020 at 13:19
  • Do not use C but C++. So instead C-array use std::array or std::vector. Commented Jul 10, 2020 at 13:19
  • wordBank stops existing when the function ends, like all the other variables local to the function. The pointer you return doesn't point to anything in particular anymore. Commented Jul 10, 2020 at 13:26

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

I hate vote downs without explanation, even when I'm not a victim.
Not the downvoter but beware of counter-voting.
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;
}

10 Comments

I have no idea why we permanently answer duplicates...
Seems likely someone downvoted all the answers because this is a frequent duplicate question.
@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: 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.
@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.
|
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.