This question already has an answer here:
- No matching function - ifstream open() 1 answer
i'm trying to pass a string from main to another function. this string is a name of text file that needs to be oepened. as far as i can see, i am passing the string alright, but when i try to use ifstream.open(textFileName)
, it doesn't quite work. but when i manually hardcode it as ifstream.open("foo.txt")
, it works just fine. i would need to use this function several times so i would like to be able to pass in a string of text file name..
here's my main
#ifndef DATA_H
#define DATA_H
#include "Data.h"
#endif
#ifndef DATAREADER_H
#define DATAREADER_H
#include "DataReader.h"
#endif
using namespace std;
int main()
{
vector<Data*> database = DataReader("foo.txt");
return 0;
}
header of DataReader
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#ifndef DATA_H
#define DATA_H
#include "Data.h"
#endif
using namespace std;
vector<Data*> DataReader(string textFile);
and finally the DataReader.cpp
#include "DataReader.h"
using namespace std;
vector<Data*> DataReader(string textFile)
{
ifstream aStream;
aStream.open(textFile); //line 11
i looked up the ifstream.open() and it takes a string and a mode as parameters. not really sure what to do with the modes, but i tried them but they gave the same error message
DataReader.cpp: In function 'std::vector<Data*, std::allocator<Data*> > DataReader(std::string)':
DataReader.cpp:11: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::open(std::string&)'
/usr/local/lib/gcc/sparc-sun-solaris2.9/4.0.3/../../../../include/c++/4.0.3/fstream:495: note: candidates are: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
thank you in advance for any input/suggestions.
Dean