一、字符串操作
1、使用空的char*指针构造std::string。
出现运行时错误:terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct NULL not valid。
std::string str = pszStr;//赋值前需要检查pszStr是否为空
2、类型定义
typedef unsigned char BYTE;
这句代码编译时提示错误如下:
longpackage.h:18: error: duplicate ‘unsigned’
longpackage.h:18: error: multiple types in one declaration
longpackage.h:18: error: declaration does not declare anything
查证后原因为:BYTE类型重复。
解决方法: 1、使用#ifndef检查后再定义类型。
2、使用#undef BYTE取消已有定义。
3、在写一个namespace内代码时,编译出现错误:error C2143: syntax error : missing ';' before 'namespace'。
定义如下:
#include "ref_count_t.h"
#include <string.h>
namespace bf_com_util //编译提示这一行出错
{
xxxxx函数定义
}
排查发现:和第一个include头文件有关。
在ref_count.h中定义了一个类(无论是普通类还是模板类都一样)
class Test
{
....//无论任何函数都不影响
};//问题位置
类定义最后的分号";"决定了一个编译错误。如果没有分号就提示上面的编译错误。(原因待补充)