c++: ifstream open problem with passing a string for text file name [duplicate]

拟墨画扇 提交于 2019-11-28 09:54:54

the standard streams doesn't accept a standard string, only c-string! So pass the string using c_str():

aStream.open(textFile.c_str());

Try this:

aStream.open(textFile.c_str()); //line 11

I think your code needs to take the internal C string to pass to the open() call. Note I'm not at a compiler right now, so can't double check this.

You may also want to check the signature of the this method:

vector<Data*> DataReader(string textFile);

Here, a complete copy of the vector will be taken when it's returned from the method, which could be computationally expensive. Note, it won't copy the Data objects, just the pointers, but with a lot of data might not be a good idea. Similarly with the string input.

Consider this instead:

void DataReader( const string& textFile, vector<Data*>& dataOut );

ifstream open takes a const char* pointer as parameter, use c_str() function of std::string to get this pointer. You can see the meaning of the parameters here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!