最近在使用c++解析caffe的prototxt文件,编码能力有限,然后就遇到了一些问题:
记载如下:
1./home/fuxueping/sdb/TensorRT/code/Parse_file/parse_pt.cpp:97: error: no match for ‘operator!=’ (operand types are ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ and ‘long int’)
if((layer_name != NULL) && (layer_type != NULL))
^
报错的语句为:
string layer_name = NULL;
string layer_type = NULL;
if((layer_name != NULL) && (layer_type != NULL))
{
/*my code*/
}
解决方法:
std::string 字符串不可以初始化为NULL,虽然能通过编译,但是会出现运行错误。
std::string 使用以下两种初始化为空的方式:
std::string s;//执行默认构造函数
std::string s = "";//执行拷贝构造函数
std::string 不可以与null相比较,可以与”“比较,使用empty()函数,length() == 0
2. error: no matching function for call to ‘std::basic_fstream<char>::open(const string&, const openmode&)’
报错的语句:
int Parse_Pt::WriteFile(const vector<string>& s, const string& fileName) {
fstream t;
t.open(fileName, ios::out);
/*my code*/
}
解决方法:
原因是C++的string类无法作为open的参数。这样修改代码:
int Parse_Pt::WriteFile(const vector<string>& s, const string& fileName) {
fstream t;
t.open(fileName.c_str(), ios::out);
/*my code*/
}
参考文献: https://blog.csdn.net/u010821666/article/details/77934510
https://blog.csdn.net/tdmyl/article/details/39056041