0

Say I have the following code:
class.h

#ifndef CLASS_H
#define CLASS_H

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

#endif

class.cpp

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

Compiling this results in the following error:

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

I am however able to use the std::regex library in any other manner? Running on GCC 6.1.1. Also tried to compile explicitly with the -std=C++11 flag.

Jzk
  • 13
  • 1
  • 3

2 Answers2

4
#ifndef CLASS_H
#define CLASS_H

#include <regex>

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

#endif

Works fine if you actually include the library in your class.

Example

deW1
  • 5,562
  • 10
  • 38
  • 54
0

For using a class you have several ways: The first way is including header file of the class before first usage. If you do not want to include the header you can use forward declaration but in the case of std classes, it can cause an undefined behavior. Here is an example of using forward declaration for std classes: Forward declare an STL container?

Community
  • 1
  • 1
Sam Mokari
  • 461
  • 3
  • 11