【问题标题】:Why is std::regex not a valid type?为什么 std::regex 不是有效类型?
【发布时间】:2017-01-17 01:17:53
【问题描述】:

假设我有以下代码:
class.h

#ifndef CLASS_H
#define CLASS_H

class Class
{
    private:
        std::regex* regex;
};

#endif

class.cpp

#include <regex>
#include "class.h"
// ...

编译它会导致以下错误:

error: "regex" in namespace "std" does not name a type
    std::regex* regex;
         ^~~~~

但是,我能够以任何其他方式使用 std::regex 库吗? 在 GCC 6.1.1 上运行。还尝试使用 -std=C++11 标志显式编译。

【问题讨论】:

  • #include &lt;regex&gt; in class.h
  • @PeteBecker basic_regex 是类模板,regex 不是。
  • @songyuanyao 我不敢相信我错过了

标签: c++ regex c++11 g++ libstdc++


【解决方案1】:
#ifndef CLASS_H
#define CLASS_H

#include <regex>

class Class
{
    private:
        std::regex* regex;
};

#endif

如果你真的在你的类中包含了这个库,那就可以了。

Example

【讨论】:

    【解决方案2】:

    要使用一个类,你有几种方法: 第一种方法是在第一次使用之前包含类的头文件。 如果您不想包含标头,则可以使用 forward declaration 但对于 std 类,它可能会导致未定义的行为。 以下是对 std 类使用前向声明的示例: Forward declare an STL container?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      • 1970-01-01
      • 2016-08-06
      相关资源
      最近更新 更多