Cannot use make_constructor in boost::python when declaring external constructor - boost-python

I'm trying to defined an external constructor when porting a class to python, by using make_constructor absolutely fails. When I try:
#include <boost/python/numpy.hpp>
using boost::python;
class foo
{
int i;
public:
foo(int i) : i(i){}
};
foo foo_create(int i){return foo(i);}
BOOST_PYTHON_MODULE(bar)
{
class_<foo>("foo")
.def("__init__", make_constructor(&foo_create));
}
I get the following error
error: no type named ‘element_type’ in ‘class foo’
I tried using noinit and init() with the same result. What am I doing wrong?

Awe found the problem, part of it being the really sparse documentation on make_construction. I needed to return a ptr to a new instance like so (in this case I made them shared pointers):
#include <boost/python/numpy.hpp>
#include <memory>
using boost::python;
class foo
{
int i;
public:
foo(int i) : i(i){}
};
std::shared_ptr<foo> foo_create(int i){return std::shared_ptr<foo>(foo(i));}
BOOST_PYTHON_MODULE(bar)
{
class_<foo, std::shared_ptr<foo>>("foo")
.def("__init__", make_constructor(&foo_create));
}
The documentation on make_constructor is really sparse, but there is some discussion here: https://wiki.python.org/moin/boost.python/HowTo under point "9".

Related

unknown type name on defining object of another class from same file in C++

In my one c++ file named "two_elements.cpp", I am defining two classes within same file, and the code goes like below:
#include <iostream>
#include <cstream>
class A{
public:
void methodA(){
//do something
}
};
class B{
public:
A a_obj;
a_obj.methodA();
};
This throws the following error:
error: unknown type name 'a_obj'
I folowed the following stack overflow links comprehensively, but could not find a work around:
Unknown type name class
unknown type name 'class'
C++ - 2 classes 1 file
Please note most of these questions had classes in the individual header files.
I found the solution:
#include <iostream>
#include <cstream>
class A{
public:
void methodA(){
//do something
}
};
class B{
public:
A a_obj;
B(){
a_obj.methodA();
}
};
An instance needs to be called inside a constructor or a function in c++.

Classes (located in different headers) that use each other's objects

The class called Universes uses a data member of type States, whilst States uses an object of type Universes. I'm using Visual C++ 2010 Express (if that makes any difference).
States.h:
class Universes;
extern Universes universe;
class States
{
public:
int relations;
States();
};
States::States()
{
relations = universe.state_no;
}
Universes.h
#include "States.h"
class Universes
{
public:
States state;
int state_no;
};
Test.cpp
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include "Universes.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Universes universe;
_getch();
return 0;
}
I keep getting the following errors:
States.h(16): error C2027: use of undefined type 'Universes'
States.h(1) : see declaration of 'Universes'
States.h(16): error C2228: left of '.state_no' must have class/struct/union
At the point where you try to access universe.state_no, the Universes class is incomplete (it's forward-declared).
A clean way to fix this is to move the definition of States::States into States.cpp, and make sure States.cpp #includes Universes.h.

boost thread+signals: mem_fn error, invalid use of non-static member function

I am trying to get acquainted with boost thread and signals. I have therefore implemented this very simple code consisting of a class (Class1) implementing a thread. I'd like this class to provide services as result of signals reception. To this end I have just started to exploit the signal boost library but I am getting this error:
/home/andrea/libs/boost_1_50_0/boost/bind/mem_fn.hpp:359:22: error: invalid use of non-static member function
when I try to compile it in the Eclipse environment with gcc. Is there anything wrong with the singleton or is the binding to the instance method?
Here is Class1.cpp
#include "Class1.hpp"
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
#include "Package1.hpp"
Class1::Class1(){
boost::thread thread(boost::bind(&Class1::classifierBehavior,this));
};
void Class1::classifierBehavior(){
service.run();
Package1Signals::getInstance()->signal1.connect(boost::bind(&Class1::method1, boost::ref(*this)));
};
void Class1::method1(Signal1 signal1){}
And Package1.hpp
#ifndef PACKAGE1_HEADER
#define PACKAGE1_HEADER
#include <boost/signal.hpp>
struct Signal1{
int foo;
};
class Package1Signals{
private:
Package1Signals();
static Package1Signals * instance;
public:
boost::signal<void (Signal1)> signal1;
static Package1Signals * getInstance(){
if(!instance){
instance = new Package1Signals();
}
return instance;
};
};
#endif
Your binder should have 1 argument:
boost::bind(&Class1::methpod1, boost::ref(*this), _1)

How to get a class member to behave like a function pointer using Boost

I would like to have a class member function behave like a function pointer. I need this behavior to integrate my own classes into some existing code.
It seems that this may be possible using Boost::function and Boost::bind, but I can't seem to get it working. The following code is a minimal example that I am using to test my implementation. The last line in the main() program is what I would like to be able to do.
Any help is greatly appreciated. I am using g++ and Boost 1.46.
// Includes
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <stdio.h>
using namespace std;
// Define a pure virtual base class
class Base{
public:
virtual double value(double v, double t) = 0;
};
// Define a derived class
class Derived : public Base{
public:
double value(double v, double t){
return v*t;
}
};
// Main program
int main(){
// A derived class
boost::shared_ptr<Derived> p(new Derived);
// Use class directly
printf("value = %f\n", p->value(100, 1));
// Create a boost::function
boost::function< double (Derived*, double, double) > f;
f = &Derived::value;
printf("f(&A, 100, 2) = %f\n", f(p.get(), 100, 2));
// Use boost::bind
printf("bind f(100,3) = %f\n", boost::bind(&Derived::value, p, _1, _2)(100,3));
// Make a boost::function to the binded operation???
boost::function< double (double, double) > f2;
f2 = boost::bind(&Derived::value, p.get()); // This is wrong
printf("f2(100,4) = %f\n", f2(100,4)); // I want to be able to do this!
}
Based on the documentation (See section "Using bind with pointers to members"), you need to specify that the function has two parameters:
f2=bind(&Derived::value, p.get(), _1, _2);
f2(100, 4); // p.get()->value(100, 4)

C++ Class Declaration

Why does the following program give me an declaration error?
Aren't I declaring it at that specific line?
#include <iostream>
#define MILLION 1000000
using namespace std;
class BitInt
{
public:
BigInt();
private:
int digit_array[MILLION];
int length;
};
BigInt::BigInt()
{
int length=0;
for(int i=0; i<MILLION; i++)
digit_array[i]=0;
}
int main()
{
BigInt();
return 0;
}
bigint.cpp:11: error: ISO C++ forbids declaration of ‘BigInt’ with no type
bigint.cpp:18: error: ‘BigInt’ has not been declared
bigint.cpp:18: error: ISO C++ forbids declaration of ‘BigInt’ with no type
bigint.cpp: In function ‘int BigInt()’:
bigint.cpp:22: error: ‘digit_array’ was not declared in this scope
You misspelled "BigInt" for "BitInt":
class BitInt
The class is named "BitInt" when I presume it should be "BigInt". Just a typo.
This is your problem:
int main()
{
BigInt(); // <--- makes no sense
return 0;
}
It should be:
int main()
{
BigInt bigint; // create object of a class
return 0;
}
And you are declaring class BitInt and in main using BigInt - there's typo one is Bit the other Big
On an unrelated note, defining MILLION as 1000000 is pointless. The reason to use named constants is to make the purpose of the number clear and allow you to change it easily, not just to let you type a number out in words instead of numerals.
It'd be better to call the constant BIGINT_DIGITS or something.