0

I have got a toy class from tutorialspoint in a file test.h:

class Box {
   public:

      Box(int l, int b, int h)
      {
        length = l;
        breadth = b;
        height = h;       
      }

      double getVolume(void) {
         return length * breadth * height;
      }
      void setLength( double len ) {
         length = len;
      }
      void setBreadth( double bre ) {
         breadth = bre;
      }
      void setHeight( double hei ) {
         height = hei;
      }

   private:
      double length;      // Length of a box
      double breadth;     // Breadth of a box
      double height;      // Height of a box
};

In the another file I have:

BOOST_PYTHON_MODULE(test)
{
  namespace python = boost::python;

  python::class_<Box>("Box")
    .def("setLength",  &Box::setLength )
    .def("setBreadth", &Box::setBreadth)
    .def("setHeight",  &Box::setHeight )
    .def("getVolume",  &Box::getVolume );
}

When I compile this code I get the error message about the Box class constructor:

/usr/include/boost/python/object/value_holder.hpp:133:13: error: no matching function for call to ‘Box::Box()’
             BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_UNFORWARD_LOCAL, nil)
             ^

What am I missing?

Do I need to write the constructor paramaters in BOOST_PYTHON_MODULE()? If so, how to do it?

2 Answers 2

2

You dont have a default constructor and you are missing the one you declared:

BOOST_PYTHON_MODULE(test) {
  namespace python = boost::python;

  python::class_<Box>("Box", boost::python::init<int, int, int>())
    .def("setLength",  &Box::setLength )
    .def("setBreadth", &Box::setBreadth)
    .def("setHeight",  &Box::setHeight )
    .def("getVolume",  &Box::getVolume );
}
Sign up to request clarification or add additional context in comments.

4 Comments

I do not understand either @YSC, this fixes the problem.
Actually the answer, better than @YSC suggestion ( as it changes the contract of the object, sorry!).
error: ‘init’ is not a member of ‘boost’ python::class_<Box>("Box", boost::init<int, int, int>())
Sorry, the init function is defined in the boost::python namespace. I just updated the answer. @PintoDoido
1

The compiler is complaining that Box doesn't provide a default constructor BOOST_PYTHON_MODULE needs:

no matching function for call to ‘Box::Box()

Simply define one:

class Box {
public:
    Box() = default;
// [...]
};

Additionnaly, you can checkout mohabouje's answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.