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;
}