首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

该死的string和char *解决方案

2012-03-08 
该死的string和char *我有一个程序,引用了第三方的库里面的函数,改函数接口如下:query(dataFiledata,char*

该死的string和char *
我有一个程序,引用了第三方的库里面的函数,改函数接口如下:
query(dataFile   data,   char   *query,char   *order)

我在构建query的时候,为了灵活,先用的string类型的变量stringQuery,可是在调用这个函数的时候,我用stringQuery.c_str()把string类型变为char   *类型,但是这里报错了,系统提示说,不能把把const   char*   变成char   *类型,这个是怎么回事?把程序附上大家分析一下!

#include   "stdafx.h "
#include   "d4all.hpp "
#include   <iostream>
#include   <fstream>
#include   <string>
#include   <cstdlib>

using   namespace   std;


#include   "d4all.hpp "    
#include   <iostream>
using   namespace   std;
extern   unsigned   _stklen   =   10000;    

Code4       codeBase   ;  
Data4       student   ;    
void   openDataFiles()   ;
void   printRecord(Data4   dataFile   )   ;
void   query(   Data4   dataFile,     string   expr,   char   *order   =   NULL   )   ;
void   createIndexFile();

void   main(   void   )  
{          
openDataFiles(   )   ;            
//query(   student,   "AGE   >   30 "   )   ;  
//query(   student,   "AGE   >   30 "   )   ;
string   stringQuery= " ";
for   (char   ch= 'a ';   ch <=   'z ';   ch++)
{
stringQuery   =   "\ "   \ ' "   ;
stringQuery   =   stringQuery   +   ch   +   "\ '   $   Name_py\ " "   ;
}

cout < <stringQuery.c_str() < <endl;

query(   student,   stringQuery   )   ;
query(   student,   "   'AB '   $   Name_py   ")   ;
codeBase.closeAll(   )   ;  
codeBase.initUndo(   )   ;
}  

void   openDataFiles()  
{          
              student.open(codeBase,   "D:\\spacemachine_material\\GD.dbf "   )   ;          
              codeBase.exitTest(   )   ;  
}    

void   printRecord(Data4   dataFile   )  
{        
cout < <   dataFile.recNo() < <endl;
}    

void   createIndexFile(   )
{
string   temp1   =   "D:\\spacemachine_material\\indexFiles\\ "   ;

}

void   query(   Data4   dataFile,   string   expr,   char   *order     )  
{          
Relate4set   relation(   dataFile   )   ;          
if(   !   relation.isValid(   )   )   codeBase.exit(   )   ;  
cout   < <   expr.c_str(   )   < <   endl;
cout   < <   expr.c_str(   )   < <   endl;  
relation.querySet(   expr.c_str()   )   ;    
relation.sortSet(   order   )   ;


cout < <   "记录条数为 "   < <   relation.count() < <endl   ;
system( "pause ");
       
codeBase.unlock(   )   ;
relation.free(   )   ;  
}    


如果query函数的参数直接用字符串的话,就能通过,比如直接赋值为 "   'A '   $   Name_py ",   这个怎么弄呢?


[解决办法]
用 " 'AB ' $ Name_py " 不出错的话,const_cast <char *> ( expr.c_str() ) 也可以

偶宁愿 strdup + free 。。。
[解决办法]
有这个问题么 ?
[解决办法]
void query( Data4 dataFile, string expr, char *order )
你把这个原型变一下:

void query( Data4 dataFile, string expr, const char *order )
[解决办法]
理论上,只有vector <char> 可以转换成char *
实际中,string的c_str()再强转char *也不会出错。
[解决办法]
string.c_str()的返回值是const char*,目的是为了防止在string之外对string的内容进行修改,导致错误,因为c_str()是直接返回string的internal buffer

如果你的query(dataFile data, char *query,char *order)不需要对query和order参数进行改变,那么就把这个参数改称const char*

如果不能保证不会改变,那就只好把c_str复制一份了

热点排行