C++ 实例化basic_regex类型的数组时出错

C++ 实例化basic_regex类型的数组时出错,c++,C++,我试了一整天都没有运气 这项工作: std::regex pattern ("Test"); 这不起作用: std::regex pattern_array[2] {"Test1", "Test2"}; 生成错误: mainprog.cpp:534:47: error: could not convert ‘(const char*)"Test1"’ from ‘const char*’ to ‘std::regex {aka std::basic_regex<char>}’

我试了一整天都没有运气

这项工作:

std::regex pattern ("Test");
这不起作用:

std::regex pattern_array[2] {"Test1", "Test2"};
生成错误:

mainprog.cpp:534:47: error: could not convert ‘(const char*)"Test1"’ from ‘const char*’ to ‘std::regex {aka std::basic_regex<char>}’

mainprog.cpp:534:47: error: could not convert ‘(const char*)"Test2"’ from ‘const char*’ to ‘std::regex {aka std::basic_regex<char>}’
试试这个

std::regex pattern_array[2] = { std::regex("Test1"), std::regex("Test2") };

您需要使用构造函数显式构造,因为正则表达式类在其构造函数上使用
explicit
关键字

注意
明确的部分。注意!!重新创建“问题”。谢谢你们两位帮助我。谢谢,这很有效。有没有想过为什么我的解决方案失败了?奇怪的是,它适用于单个(非数组)对象。这是应该匹配的构造函数:显式基本正则表达式(const CharT*s,flag_type f=std::regex_constants::ECMAScript);这对我的应用程序来说并不重要,但是避免使用2个额外的正则表达式对象会很好。再次感谢。这真的会导致复制构造吗?如果是这样,您可以分别构造它们(每行一个),然后拥有一个指向构造对象的两个指针数组。原始版本失败,因为它依赖于从const char*到regex的隐式转换。可能它不调用副本构造函数。我会检查一下的。唯一困扰我的是我的“重建”工作。如果您想看一看,我将把代码作为回复发布。您的testclass代码可以工作,因为构造函数没有声明为显式的。看到了,谢谢。我现在在自己的课堂上也犯了同样的错误(这很好)。我一定会记住那个关键词。。。
std::regex pattern_array[2] = { std::regex("Test1"), std::regex("Test2") };