gpt4 book ai didi

C++ 需要类型说明符(正则表达式)

转载 作者:行者123 更新时间:2023-11-28 03:29:03 24 4
gpt4 key购买 nike

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <regex>

using namespace std;

class Parser {
protected:
// regex: containers
static const regex rxProc("procedure\s+([\w]+)\s*{");
}

"procedure\s+([\w]+)\s*{" 部分出现错误。需要一个类型说明符。我一定做错了什么?

最佳答案

查看此处的其他答案以了解您实际向编译器询问的内容。如果你想在你的类中创建一个常量 - 我在这里提供了一个解决方案。

在 C++ 中使用静态成员很有趣,但仅限于 POD(纯旧数据)成员。对于更复杂的成员,事情变得很难看。即使对于 char* 这也不会扩大。使用良好的旧 C #define 有时是个好主意 - 即使在 C++ 中也是如此;

像这样的东西会解决你的编译问题。使用 #if 0 触发您的旧代码,并使用 g++ -std=gnu++0x -c test1.cpp

进行编译
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <regex>

using namespace std;

#if 0
// my fix

// in your H file
class Parser {
protected:
// regex: containers
static const regex rxProc;
};
// in your CPP file
const regex Parser::rxProc = regex("procedure\\s+([\\w]+)\\s*{");
#else

// your old code
class Parser {
protected:
// regex: containers
static const regex rxProc("procedure\\s+([\\w]+)\\s*{");
};
#endif

(甚至没有涉及使用正则表达式定义语法树的问题...或解析 Pascal 文件...这只是个坏主意,会破坏哦,所以,很多次...只是不要)

关于C++ 需要类型说明符(正则表达式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13117246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com