Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

<vector> back() throws an instance of 'std::out_of_range&#

$
0
0
I have defined:
vector <string> _contents;
int _lines;
ifstream* _file;
string _path;

And I read content from a text file using
bool File::_refresh(const string path)
{
	_path = path;
	_file = new ifstream(_path.c_str());
	if (_file->is_open() != true)
	{
		_file = NULL;
		return false;
	}
	//no of lines
	_lines = 0;
	_contents.clear();
	while (_file->eof() == false)
	{
		string cur_line;
		getline(*_file, cur_line);
		_contents.push_back(cur_line);
		_lines += 1;
	}
	_file->close();
	return true;
}


Why then should
cout << _contents.back() << endl;

throw an 'std::out_of_range' at runtime?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>