string str=NULL?

terminate called after throwing an instance of 'std::logic_error'
 what():  basic_string::_S_construct NULL not valid

已放弃

原因在于没搞清, char *型的变量和指向 c 风格的字符串指针的区别.

char *cp = NULL;     cp是一个char*变量

char *str = "Hello";  str是一个指向 c 风格的字符串指针.

std::string 的构造函数需要的是一个 c 风格的字符串指针(要求以字符 null 结尾), 而不是一个char *的变量. 所以运行时出了上述错误.

另外, 很多 c 标准函数, 函数明确说明, 需要一个 c 风格的字符串. 虽然char * 和指向 c 风格的字符串指针的代码看起来都是 char *, 但是含义上有本质的区别.

posted on 2013-03-25 16:02  makeup1122  阅读(321)  评论(0编辑  收藏  举报

导航