解决C2143语法错误: 缺少“;”(在“<end Parse>”的前面)

本文介绍了一种在Visual Studio 2015中遇到的奇怪编译错误C2143,并详细解释了如何通过更改模板类静态成员变量的初始化方式来解决这个问题。

最近在编译一个程序时出现了一个很奇葩的编译错误,错误代码为C2143,错误说明为“语法错误: 缺少“;”(在“<end Parse>”的前面)”。看到这个错误我就卧槽了,<end Parse>是什么鬼?编译器提示错误位置在第2054行,而对应源文件的总行数只有2053行,这个2054行是从哪冒出来的?一开始我以为是文件中有特殊字符,把大量代码删除之后却编译成功,于是我怀疑是代码问题。我使用注释大法逐一进行编译测试,发现问题所在:

问题代码长得像这样子,在visual studio 2015下出现编译错误:

template<typename t> class C {
public:
	C(int) {}
	static C c;
};
template<typename t> C<t> C<t>::c{1};
int main() {
	C<int>::c;//C2143
}

要再现这个错误需要具备2个条件:

  • 模板类的静态成员变量使用花括号初始化
  • 实例化模板

解决问题的方法很简单,把初始化改用圆括号即可。

template<typename t> const C<t> C<t>::c(1);



#include <iostream> #include <string> #include <regex> #include <unordered_map> // 定义正则表达式模式来匹配 Type 后的标识符及其对应的数值表达式 std::unordered_map<std::string,double> parseExpression(const std::string& input) { // 正则表达式模式:匹配 "if(TYPE == "X")(...)" 形式的表达式 std::regex pattern(R"(if\(TYPE\s*==\s*["']([A-Z])["']\)\(([\d.]+)\))"); std::smatch matches; std::unordered_map<std::string,double> result; auto start = input.begin(); while(std::regex_search(start,input.end(),matches,pattern)) { if(matches.size() == 3) { std::string type = matches[1].str(); double value = std::stod(matches[2].str()); result[type] = value; start = matches.suffix().first; } else { start = matches.suffix().first; } } return result; } int main() { std::string input = R"(if(TYPE == "A")(3.00)else IF(TYPE=="E"||TYPE=="G")(1.51)ELSE(1.50)!0.01^if(TYPE == "D")(7.85)else if(TYPE == "R")(7.99)else(8.00)!0.01)"; auto parsed = parseExpression(input); for(const auto& pair : parsed) { std::cout << "Type: " << pair.first << ", Value: " << pair.second << std::endl; } return 0; }1>test2.cpp 1>test2.cpp(10): warning C4129: “(”: 不可识别的字符转义序列 1>test2.cpp(10): warning C4129: “s”: 不可识别的字符转义序列 1>test2.cpp(10): warning C4129: “s”: 不可识别的字符转义序列 1>test2.cpp(10): error C2065: “R”: 未声明的标识符 1>test2.cpp(10): error C2143: 语法错误 : 缺少)”(在“字符串”的前面) 1>test2.cpp(10): error C2015: 常量中的字符太多 1>test2.cpp(10): error C2017: 非法的转义序列 1>test2.cpp(10): error C2059: 语法错误:)” 1>test2.cpp(10): error C2017: 非法的转义序列 1>test2.cpp(10): error C2017: 非法的转义序列 1>test2.cpp(10): error C2017: 非法的转义序列 1>test2.cpp(10): error C2059: 语法错误:)” 1>test2.cpp(10): error C2001: 常量中有换行符 1>test2.cpp(16): error C2065: “matches”: 未声明的标识符 1>test2.cpp(16): fatal error C1903: 无法从以前的错误中恢复;正在停止编译 1>已完成生成项目“test2.vcxproj”的操作 - 失败。
06-07
已启动生成… 1>------ 已启动生成: 项目: Project1, 配置: Release Win32 ------ 1>TTF Chinese Check.c 1>G:\TTF-Test\Project1\TTF Chinese Check.c(8,5): error C2061: 语法错误: 标识符“uint32_t” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(9,14): error C2061: 语法错误: 标识符“numTables” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(9,14): error C2059: 语法错误:“;” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(10,14): error C2061: 语法错误: 标识符“searchRange” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(10,14): error C2059: 语法错误:“;” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(11,14): error C2061: 语法错误: 标识符“entrySelector” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(11,14): error C2059: 语法错误:“;” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(12,14): error C2061: 语法错误: 标识符“rangeShift” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(12,14): error C2059: 语法错误:“;” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(13,1): error C2059: 语法错误:“}” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(17,5): error C2061: 语法错误: 标识符“uint32_t” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(18,14): error C2061: 语法错误: 标识符“offset” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(18,14): error C2059: 语法错误:“;” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(19,14): error C2061: 语法错误: 标识符“length” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(19,14): error C2059: 语法错误:“;” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(20,1): error C2059: 语法错误:“}” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(23,5): error C2061: 语法错误: 标识符“uint16_t” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(24,14): error C2061: 语法错误: 标识符“encodingID” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(24,14): error C2059: 语法错误:“;” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(25,14): error C2061: 语法错误: 标识符“languageID” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(25,14): error C2059: 语法错误:“;” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(26,14): error C2061: 语法错误: 标识符“nameID” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(26,14): error C2059: 语法错误:“;” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(27,14): error C2061: 语法错误: 标识符“stringLength” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(27,14): error C2059: 语法错误:“;” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(28,14): error C2061: 语法错误: 标识符“stringOffset” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(28,14): error C2059: 语法错误:“;” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(29,1): error C2059: 语法错误:“}” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(34,10): error C2061: 语法错误: 标识符“read_uint16” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(34,10): error C2059: 语法错误:“;” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(34,26): error C2059: 语法错误:“<parameter-list>” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(40,10): error C2061: 语法错误: 标识符“read_uint32” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(40,10): error C2059: 语法错误:“;” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(40,26): error C2059: 语法错误:“<parameter-list>” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(50,51): error C2143: 语法错误: 缺少)”(在“*”的前面) 1>G:\TTF-Test\Project1\TTF Chinese Check.c(50,51): error C2081: “uint32_t”: 形参表中的名称非法 1>G:\TTF-Test\Project1\TTF Chinese Check.c(50,51): error C2143: 语法错误: 缺少“{”(在“*”的前面) 1>G:\TTF-Test\Project1\TTF Chinese Check.c(50,69): error C2143: 语法错误: 缺少“;”(在“*”的前面) 1>G:\TTF-Test\Project1\TTF Chinese Check.c(50,77): error C2059: 语法错误:)” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(50,79): error C2054: 在“length”之后应输入“(” 1>G:\TTF-Test\Project1\TTF Chinese Check.c(79,16): error C2065: “TTF_Header”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(79,16): error C2146: 语法错误: 缺少“;”(在标识符“header”的前面) 1>G:\TTF-Test\Project1\TTF Chinese Check.c(79,22): error C2065: “header”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(80,11): error C2065: “header”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(80,12): error C2224: “.sfntVersion”的左侧必须具有结构/联合类型 1>G:\TTF-Test\Project1\TTF Chinese Check.c(81,11): error C2065: “header”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(81,12): error C2224: “.numTables”的左侧必须具有结构/联合类型 1>G:\TTF-Test\Project1\TTF Chinese Check.c(82,11): error C2065: “header”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(82,12): error C2224: “.searchRange”的左侧必须具有结构/联合类型 1>G:\TTF-Test\Project1\TTF Chinese Check.c(83,11): error C2065: “header”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(83,12): error C2224: “.entrySelector”的左侧必须具有结构/联合类型 1>G:\TTF-Test\Project1\TTF Chinese Check.c(84,11): error C2065: “header”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(84,12): error C2224: “.rangeShift”的左侧必须具有结构/联合类型 1>G:\TTF-Test\Project1\TTF Chinese Check.c(86,31): error C2065: “header”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(86,32): error C2224: “.numTables”的左侧必须具有结构/联合类型 1>G:\TTF-Test\Project1\TTF Chinese Check.c(87,22): error C2065: “TTF_TableDir”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(87,22): error C2146: 语法错误: 缺少“;”(在标识符“dir”的前面) 1>G:\TTF-Test\Project1\TTF Chinese Check.c(87,25): error C2065: “dir”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(88,18): error C2065: “dir”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(88,19): error C2224: “.tag”的左侧必须具有结构/联合类型 1>G:\TTF-Test\Project1\TTF Chinese Check.c(88,14): error C2198: “fread”: 用于调用的参数太少 1>G:\TTF-Test\Project1\TTF Chinese Check.c(89,12): error C2065: “dir”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(89,13): error C2224: “.checkSum”的左侧必须具有结构/联合类型 1>G:\TTF-Test\Project1\TTF Chinese Check.c(90,12): error C2065: “dir”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(90,13): error C2224: “.offset”的左侧必须具有结构/联合类型 1>G:\TTF-Test\Project1\TTF Chinese Check.c(91,12): error C2065: “dir”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(91,13): error C2224: “.length”的左侧必须具有结构/联合类型 1>G:\TTF-Test\Project1\TTF Chinese Check.c(94,16): error C2065: “dir”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(94,17): error C2224: “.tag”的左侧必须具有结构/联合类型 1>G:\TTF-Test\Project1\TTF Chinese Check.c(94,25): error C2065: “dir”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(94,26): error C2224: “.offset”的左侧必须具有结构/联合类型 1>G:\TTF-Test\Project1\TTF Chinese Check.c(94,37): error C2065: “dir”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(94,38): error C2224: “.length”的左侧必须具有结构/联合类型 1>G:\TTF-Test\Project1\TTF Chinese Check.c(101,14): error C2146: 语法错误: 缺少“;”(在标识符“name_offset”的前面) 1>G:\TTF-Test\Project1\TTF Chinese Check.c(101,25): error C2065: “name_offset”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(101,38): error C2065: “name_length”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(102,45): error C2065: “name_offset”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(102,59): error C2065: “name_length”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(107,26): error C2065: “name_offset”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(108,14): error C2065: “uint16_t”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(108,14): error C2146: 语法错误: 缺少“;”(在标识符“format”的前面) 1>G:\TTF-Test\Project1\TTF Chinese Check.c(108,21): error C2065: “format”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(109,14): error C2065: “uint16_t”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(109,14): error C2146: 语法错误: 缺少“;”(在标识符“count”的前面) 1>G:\TTF-Test\Project1\TTF Chinese Check.c(109,20): error C2065: “count”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(110,14): error C2065: “uint16_t”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(110,14): error C2146: 语法错误: 缺少“;”(在标识符“stringOffsetBase”的前面) 1>G:\TTF-Test\Project1\TTF Chinese Check.c(110,31): error C2065: “stringOffsetBase”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(114,30): error C2065: “count”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(115,24): error C2065: “TTF_NameRecord”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(115,24): error C2146: 语法错误: 缺少“;”(在标识符“rec”的前面) 1>G:\TTF-Test\Project1\TTF Chinese Check.c(115,27): error C2065: “rec”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(116,12): error C2065: “rec”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(116,13): error C2224: “.platformID”的左侧必须具有结构/联合类型 1>G:\TTF-Test\Project1\TTF Chinese Check.c(117,12): error C2065: “rec”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(117,13): error C2224: “.encodingID”的左侧必须具有结构/联合类型 1>G:\TTF-Test\Project1\TTF Chinese Check.c(118,12): error C2065: “rec”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(118,13): error C2224: “.languageID”的左侧必须具有结构/联合类型 1>G:\TTF-Test\Project1\TTF Chinese Check.c(119,12): error C2065: “rec”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(119,13): error C2224: “.nameID”的左侧必须具有结构/联合类型 1>G:\TTF-Test\Project1\TTF Chinese Check.c(120,12): error C2065: “rec”: 未声明的标识符 1>G:\TTF-Test\Project1\TTF Chinese Check.c(120,12): fatal error C1003: 错误计数超过 100;正在停止编译 1>已完成生成项目“Project1.vcxproj”的操作 - 失败。 ========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
最新发布
12-15
实际输出:CONSTTK const <常量说明> INTTK int IDENFR const1 ASSIGN = INTCON 1 COMMA , <无符号整数> <整数> IDENFR const2 ASSIGN = MINU - INTCON 100 SEMICN ; <无符号整数> <整数> <常量定义> CONSTTK const CHARTK char IDENFR const3 ASSIGN = CHARCON _ SEMICN ; <常量定义> INTTK int <变量说明> ; CHARTK预期输出:CONSTTK const INTTK int IDENFR const1 ASSIGN = INTCON 1 <无符号整数> <整数> COMMA , IDENFR const2 ASSIGN = MINU - INTCON 100 <无符号整数> <整数> <常量定义> SEMICN ; CONSTTK const CHARTK char IDENFR const3 ASSIGN = CHARCON _ <常量定义> SEMICN ; <常量说明> INTTK int IDENFR change1 <变量定义> SEMICN ; CHARTK char IDENFR change3 <变量定义> SEMICN ; <变量说明> INTTK int IDENFR gets1 <声明头部> LPARENT ( INTTK int IDENFR var1 COMMA , INTTK int IDENFR var2 <参数表> RPARENT ) LBRACE { IDENFR change1 ASSIGN = IDENFR var1 <因子> <项> PLUS + IDENFR var2 <因子> <项> <表达式> <赋值语句> SEMICN ; <语句> RETURNTK return LPARENT ( IDENFR change1 <因子> <项> <表达式> RPARENT ) <返回语句> SEMICN ; <语句> <语句列> <复合语句> RBRACE } <有返回值函数定义> VOIDTK void MAINTK main LPARENT ( RPARENT ) LBRACE { PRINTFTK printf LPARENT ( STRCON Hello World <字符串> RPARENT ) <写语句> SEMICN ; <语句> PRINTFTK printf LPARENT ( IDENFR gets1 LPARENT ( INTCON 10 <无符号整数> <整数> <因子> <项> <表达式> COMMA , INTCON 20 <无符号整数> <整数> <因子> <项> <表达式> <值参数表> RPARENT ) <有返回值函数调用语句> <因子> <项> <表达式> RPARENT ) <写语句> SEMICN ; <语句> <语句列> <复合语句> RBRACE } <主函数> <程序>重新修改
11-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值