#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class Dict
{
public:
string line;
int wordcount;
string word;
vector<string> words;
Dict(string f)
{
ifstream myFile;
myFile.open(f.c_str());
if (myFile.is_open())
{
while(!myFile.eof())
{
myFile >> word;
words.push_back(word);
}
cout << endl << "Wordcount: " << wordcount << endl;
}
else
cout << "ERROR couldn't open file" << endl;
myFile.close();
}
};
int main()
{
Dict d("test.txt");
cout << words.size() << endl;
return 0;
}
I get an error that words vector was not declared in main().
How can I make this visible to the compiler, since I already defined it in the class. Once an object is instantiated and the constructor is invoked, shouldn’t words vector be created? But the compiler doesn’t notice this.
How would I fix this?
wordsis a member in yourDictobjectd: