error C2664: '__thiscall std::basic_fstream<char,struct std::char_traits<char> >::std::basic_fstream<char,struct std::char_traits<char> >(const char *,int)' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
 main()
{    fstream fileread;
    fstream fileread1;
string s,s2; field  biao1;                              //结构体名称定义为表1
     map<string,struct field>fieldtype1;       //讲所有内容对应存在一个map函数中  fstream fin("E:\\测试数据\\2电磁散射静态测量扫频数据\\数据说明.txt", fstream::in);  fileread.open("E:\\测试数据\\2电磁散射静态测量扫频数据\\YR67SAHDF.txt");
    // fileread1.open("E:\\测试数据\\2电磁散射静态测量扫频数据\\数据说明.txt");  //读取配置文件的内容//判断文件是否存在
    if(!fileread)
{
       std::cerr<<"can not open the file"<<std::endl;
} for(; getline(fileread,s); )
{   int pos1=s.find("[TASK]");
  if (pos1>=0)
  break;
}   for(; getline(fileread,s); )
  {
  int pos=s.find("[DATA]");
  if( pos>=0)
  break;
   if(*s.begin ()==' ')                                
   {
//先进行判断前排空格有几个,有空格先令num=1
         int num=1;
         while(*(s.begin ()+num)==' ')
     num++;
             
     s= s.replace (s.begin (),s.begin ()+num,"");
   }       
              if(*s.begin ()=='[')
  {
                 getline(fileread,s);
  }                   else
   {
   int pos1=s.find('=');
                   if(pos1>=0)
   {
               biao1.chinese=s.substr (0,pos1);
                       biao1.output =s.substr (pos1+1);    }
   for(; getline(fin,s2);)
   {
             int pos3;
                         pos3=s2.find ('=');
              while(s=="[结束]")
 {
                  cout << "can not find !"<<endl;
              biao1.english =' ';
          biao1.type =' ';
  fin.seekg(0, ios::beg);
          break;
 }
   
         if(s2.substr (0,pos3)==biao1.chinese )
 { 
                string a;
   a =s2.substr (pos3+1);
             int pos4=a.find ('=');
                        biao1.type =a.substr (0,pos4);
               biao1.english =a.substr (pos4+1); fin.seekg(0, ios::beg);           break;
 }
   }
   }
fieldtype1.insert(pair<string, struct field>(biao1.chinese, biao1));
}

map<string,struct field>::iterator   it; 
           for(it=fieldtype1.begin ();   it!=fieldtype1.end (); it++) 
           {  
cout <<it->second.chinese <<"  "<<it->second.english <<endl;
}}
这个是主函数,我需要将它提取出来写成函数,用主函数调用该函数,但改的过程中出现问题,
readtext(string a,string b)
{    fstream fileread;
string s,s2; field  biao1;                              //结构体名称定义为表1
     map<string,struct field>fieldtype1;       //讲所有内容对应存在一个map函数中  fstream fin(a, fstream::in);  fileread.open(b);
    // fileread1.open("E:\\测试数据\\2电磁散射静态测量扫频数据\\数据说明.txt");  //读取配置文件的内容//判断文件是否存在
    if(!fileread)
{
       std::cerr<<"can not open the file"<<std::endl;
} for(; getline(fileread,s); )
{   int pos1=s.find("[TASK]");
  if (pos1>=0)
  break;
}   for(; getline(fileread,s); )
  {
  int pos=s.find("[DATA]");
  if( pos>=0)
  break;
   if(*s.begin ()==' ')                                
   {
//先进行判断前排空格有几个,有空格先令num=1
         int num=1;
         while(*(s.begin ()+num)==' ')
     num++;
             
     s= s.replace (s.begin (),s.begin ()+num,"");
   }       
              if(*s.begin ()=='[')
  {
                 getline(fileread,s);
  }                   else
   {
   int pos1=s.find('=');
                   if(pos1>=0)
   {
               biao1.chinese=s.substr (0,pos1);
                       biao1.output =s.substr (pos1+1);    }
   for(; getline(fin,s2);)
   {
             int pos3;
                         pos3=s2.find ('=');
              while(s=="[结束]")
 {
                  cout << "can not find !"<<endl;
              biao1.english =' ';
          biao1.type =' ';
  fin.seekg(0, ios::beg);
          break;
 }
   
         if(s2.substr (0,pos3)==biao1.chinese )
 { 
                string a;
   a =s2.substr (pos3+1);
             int pos4=a.find ('=');
                        biao1.type =a.substr (0,pos4);
               biao1.english =a.substr (pos4+1); fin.seekg(0, ios::beg);           break;
 }
   }
   }
fieldtype1.insert(pair<string, struct field>(biao1.chinese, biao1));
}
出现C2664错误,请高手指点。

解决方案 »

  1.   

    cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'说的很明白了
      

  2.   

    fstream fin(a, fstream::in);fileread.open(b);出错的是这2句
      

  3.   

    第一个参数要const char*类型,你给的string类型
    xx.c_str()试试先
      

  4.   

    fstream fin(a, fstream::in);fileread.open(b);出错的是这2句
      

  5.   

    从错误提示上看,就是由于数据类型不匹配造成的,fstream fin(a.c_str(), fstream::in);不行?这样写的话错误提示是什么?
      

  6.   

    error C2664: 'void __thiscall std::basic_fstream<char,struct std::char_traits<char> >::open(const char *,int)' : cannot convert parameter 1 from 'const char *(void) const' to 'const char *'
         There is no context in which this conversion is possible
      

  7.   

    这个是不能把strng转换成const char *的错误
    你把用到string的地方,改成string.c_str()试一下
      

  8.   

    fstream fin(a, fstream::in);
    fileread.open(b);
    改成
    fstream fin(a.c_str(), fstream::in);
    fileread.open(ba.c_str());
    就Ok了,这两个地方的string都要改
      

  9.   

    谢谢了,我自己的问题,c_str后少加()