Simon
Simon

Reputation: 2246

Boost Xpressive - field ‘m_rx’ has incomplete type

I am having trouble compiling a class header which contains a member field of type sregex.

The class is defined as :

#include <boost/xpressive/xpressive_fwd.hpp>
namespace Bob
{

class RegexReplace
{
public :
    boost::xpressive::sregex m_rx;
    std::string m_str;

    RegexReplace(const char* strSearch, const char* strReplace);
};

} // namespace Bob

The class cpp file compiles without any problem, but when I include the header into a cpp file which uses the class, the compiler gives the following error :

error: field ‘m_rx’ has incomplete type ‘boost::xpressive::sregex’ {aka ‘boost::xpressive::basic_regex<__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> > >’}
boost::xpressive::sregex m_rx;

What am I doing wrong here ???

Upvotes: 0

Views: 63

Answers (1)

Niclas Larsson
Niclas Larsson

Reputation: 1317

boost::xpressive::sregex m_rx; is a instantiation and you only forward declared it by including ..._fwd.hpp, in other words it's incomplete.

#include <boost/xpressive/xpressive.hpp> should solve the issue.

If you really want to forward declare your type you'll have to change it to a pointer (non working code, looks like boost uses a static ::compile(...)):

hpp:

#include <boost/xpressive/xpressive_fwd.hpp>
namespace Bob
{

class RegexReplace
{
public :
    boost::xpressive::sregex* m_rx;
    std::string m_str;

    RegexReplace(const char* strSearch, const char* strReplace);
    ~RegexReplace();

    // @todo Implement move and copy semantics
};

} // namespace Bob

cpp:

#include <boost/xpressive/xpressive.hpp>
namespace Bob
{
  RegexReplace::RegexReplace(const char* strSearch, const char* strReplace) :
    m_rx{new boost::xpressive::sregex}
  {
    ......
  }

  RegexReplace::~RegexReplace() {
    delete m_rx;
  }
}

Upvotes: 0

Related Questions