无效的函数调用.open()c ++

时间:2015-09-16 06:41:18

标签: c++

不确定这里有什么不对,我相信函数.open()应该接受string

...但有些人认为从getDateFormated() i传回的数据不是string类型的。


string getDateFormated()
{
int month ;
int year ;
char fileName [255];

cin >> month >> year;

sprintf(fileName, "%d_%d", year, month);

return fileName;
}




void readFile ()
{
cout << "Enter month then year: ";
string fileName = getDateFormated();

ifstream readFromFile;
readFromFile.open(fileName);
}

....方法继续使用一些不相关的分支(检查.fail等)



解决方案
!!但不是我以后的!!
我纠正了这个问题,让程序以我想要的方式运行(使用.c_str()) ...但!!我很困惑,为什么第一种方式不起作用!因为一切对我来说都很好!!

void readFile () 
{
cout << "Enter month then year: ";
string fileName = getDateFormated();

ifstream readFromFile;
readFromFile.open(fileName.c_str());
}


我的错误味精

  

simpleIO.cpp:在函数'void readFile()'中:
simpleIO.cpp:102:28:   错误:没有匹配的函数用于调用   'std :: basic_ifstream :: open(std :: string&amp;)'

  readFromFile.open(文件名);                               ^
simpleIO.cpp:102:28:注意:候选人是:
在simpleIO.cpp中包含的文件中:9:0:
  /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/fstream:541:7:注意:   void std :: b asic_ifstream&lt; _CharT,   _Traits&gt; :: open(const char *,std :: ios_base :: openmode)[with _CharT = char; _Traits = std :: char_traits; std :: ios_base :: openmode = std:
  :_Ios_Openmode]
         open(const char * __s,ios_base :: openmode __mode = ios_base :: in)          ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/fstream:541:7:注意:
  没有已知的转换参数1来自'std :: string {aka   std :: basic_string}'到'c
onst   字符*”

0 个答案:

没有答案