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

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)

Related

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

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".

pointers to vectors as class members pointers to vectors in functions

I'm a noob. Using C++ in Clion
I'm building a graph of N random nodes on a Cartesian plane
I have a simple type, node (just a point) (int x, int y)
node pt(x,y)
I have a vector of N randomly generated unique points (would this be considered ordered points btw?)
vector NodeList(N);
I have a class Graph (Incomplete) which has a function GenNodelist which I have tested as a standalone program. I had a hell of a time just getting the constructor to build without compile error.
#include <iostream>
#include <vector>
#ifndef DIJKSTRA_GRAPH_H
#define DIJKSTRA_GRAPH_H
using namespace std;
class Graph {
private:
int x;
int y;
vector<int> NodeList;
int *np;
public:
//constructor
x(x),y(y),NodeList(),np(){}
void GenNodeList(vector<int> NL(), int &np) {x,y,NodeList, &np; }
void GenNodeList(vector<int> *NL, int *p);
};
#endif //DIJKSTRA_GRAPH_H
void Graph::GenNodeList(vector<int>* NL, int* p) {
.
.
} .
... code that builds and has been quasi tested
So everything builds and there's a "hello world" main program in the project. The 2 classes, (node & Graph: 2 headers and 2 cpp files) along with the main "hello world" build and run. Now from main() I wan to call the call the GenNode function from main. I just want to pass a pointer and have the list generate and sit in memory UN-mutable. right now. I'll build the graph off of this later. When I try to call the function nothing works. How can I build this list and access it from main() and Graph()?
main(){
vector<int> NL(N);
int *np;
Graph::GenNodeList( NL, np);
}
Can't seem to figure this out.
This incomplete piece of code
Graph::GenNodeList( NL, np);
is ill-formed because that method is not static. You have to access instance of class Graph for non-static members. Nothing about oop here, just language's rules.
Graph(): x(x),y(y),NodeList(),np(){}
static void GenNodeList(vector<node>* NL, node* np);
void Graph::GenNodeList(vector<node>* NL, node* p) {
int main() {
vector<node> NL(N);
vector<node>* NList;
node *np = nullptr;
NList = &NL;
Graph::GenNodeList(NList,np);
return 0;
https://github.com/Pasqualino31/Dijkstra/tree/Pasqualino31-patch-2

Shall I build a destructor in this classes?

I am currently working on building an ABM model using C++.
I have classes that have the need to interact with each other, because e.g. class B needs to examine values in class A and return some evaluation on it, which then class C might want to read. Classes need not to change other classes values, only to read from them.
Class B in my current implementation has a po
inter to a vector containing all members of Class A. The pointer is there for two order of reason: it makes easier to initialize the vector, and the vector is left in the scope of main so that I can access and loop over it, calling the members of class A for each agent.
My MCVE:
#include <iostream>
#include <vector>
using namespace std;
class A; // Forward declaration
class B{
int id,
some_value;
vector<A> * A_vec;
public:
// Overloaded constructor
B(int ID, vector<A> & PTR)
{
A_vec = & PTR;
id = ID;
some_value = 0;
};
// Copy Constructor
B( const B& that ):
id(that.id),
some_value(that.some_value)
{
// Pointer ??
};
// Non-default destructor -> uncomment leads to seg_fault
/*
~B(){ delete [] A_vec;};
*/
// Assignment operator
B& operator=(const B& that)
{
id = that.id;
some_value = that.some_value;
// Pointer ??
return *this;
};
//Methods to update different variables go here ..
void do_stuff();
};
class A{
B & class2_ref;
vector<double> o;
public:
int stuff;
// Overloaded constructor
A(int STUFF, B & REF, vector<double> O):
class2_ref(REF),
o(O)
{
stuff = STUFF;
};
// Methods to update different variables go here ..
};
void B::do_stuff()
{
int L = A_vec->size();
for(int l = 0; l<L; l++) some_value += (*A_vec)[l].stuff; // Perform some operation
};
int main(){
int I = 5; // Number of objects of A
vector<double> O(12,2); // Some numbers in here
B b(0,A_vec);
for(int i = 0; i< I; i++)
{
A a(i,b,O);
A_vec.push_back(a);
}
b.do_stuff();
cout<< "Debugging MCVE" << endl;
return 0;
}
My question then is:
Should I implement the destructor/copy constructor/assignment operator in class B? What about class A ? If so, can you please point me to the correct syntax(for the destructor the one above in comments leads to seg fault).
My understanding is that this might be one of the case in which I am happy with a "shallow" destruction of the pointer, because both class B and vector<A> will go out of scope at the return statement. class B owns the pointer, which gets destructed when it is due, and the same for vector.
But then, what about the other member from the rule of three?
There is only one object of class B planned, but I might (small chance) want to generalize later on.
if a class have a pointer type, you should implement a destructor, and i would suggest implementing a copy and an assignment operator as well, else you will be dealing with the same object from 2 different places, which could cause you some errors, for example -
void someFunction(B &b)
{
B a = b;
}
B b(0,A_vec);
someFunction(b); //After finishing someFunction, it will try to delete the vector from a , but it is the same vector you used in b.
b.do_stuff(); // Would cause a seg error
And for the destructor syntax, just delete the vector, not its content, it will use the vector default destrctor on the content:
delete A_vec
just make sure you dont use it if its not initialized, i would suggest just building a empty vector on each ctor of the class, that way you wont get a seg fault and you can use delete.

Yet another uresolved external case

first time asking.
Here's the deal:
I have a helper class (at least now it's a class) that has several math functions, that I use throughout the project.
#ifndef CUSTOM_UTILS_H
#define CUSTOM_UTILS_H
//---------------------------------------------------------
#include <stdlib.h>
#define _USE_MATH_DEFINES
#include <math.h>
class cUtil {
public:
static int utilsRandom(int from, int to);
static double utilsRandom(double from, double to);
static double giveAngle(double x, double y);
static double FoV(double cx, double cy,
double fx, double fy,
double tx, double ty,
double radius);
};
//---------------------------------------------------------
#endif
Implementation:
#define _USE_MATH_DEFINES
#include "customUtils.h"
//---------------------------------------------------------
int cUtil::utilsRandom(int from, int to) {
if (from == to) {
return from;
}
return (rand() % (to - from)) + from;
}
//---------------------------------------------------------
double cUtil::utilsRandom(double from, double to) {
...
}
//---------------------------------------------------------
double cUtil::giveAngle(double x, double y) {
...
}
//---------------------------------------------------------
double cUtil::FoV(double cx, double cy,
double fx, double fy,
double tx, double ty,
double radius) {
...
}
//---------------------------------------------------------
(removed the 3 bodies to save space for the post)
Now, when I use it , let's say, in a class called 'creature' I include the customUtils.h file in the header of 'creature'. And use any of the 4 functions like so: cUtil::func_name().
Sometimes I get an unresolved external error such as
LNK2001: unresolved external symbol "public: static double __cdecl cUtil::utilsRandom
(double,double)" (?utilsRandom#cUtil##SANNN#Z) C:\Users\Rockstrongo\Documents\Projects
\nnEvo\nnEvo\net.obj
It appears for all functions in cUtil and for all classes that use those functions.
I said it sometimes appears, because it just does that - I'd be rebuilding the project and it would resurface. To scrub it again I would change some part of cUtils code, or the way it is included in other classes or anything that it would get it running again. For some time, cleaning->compiling the customUtils.cpp->then building the rest worked, but not any more.
To an untrained eye like mine this appears to be completely random and I'm all out off straws to grasp. I'm using Microsoft Visual Studio 2010. It's a console project using openGL and glut.
I see you overloaded cUtil::utilsRandom to use doubles and ints. It's ok except for one thing: You can not change the returned type. Both overloaded version must either return an int or a double. If you must have different returned types, then do not overload the function. Rather use different function names.

Using boost::program_options with own template class possible?

I'm currently start using boost::program_options for parsing command line options as well as configuration files.
Is it possible to use own template classes as option arguments? That means, something like
#include <iostream>
#include "boost/program_options.hpp"
namespace po = boost::program_options;
template <typename T>
class MyClass
{
private:
T* m_data;
size_t m_size;
public:
MyClass( size_t size) : m_size(size) { m_data = new T[size]; }
~MyClass() { delete[] m_data; }
T get( size_t i ) { return m_data[i]; }
void set( size_t i, T value ) { m_data[i] = value; }
};
int main (int argc, const char * argv[])
{
po::options_description generic("General options");
generic.add_options() ("myclass", po::value< MyClass<int>(2) >(),
"Read MyClass");
return 0;
}
Trying to compile this I get an Semantic Issue (No matching function for call to 'value'). I guess I need to provide some casting to an generalized type but I have no real idea.
Can anybody help?
Thanks
Aeon512
I wouldn't know if boost::program_options allows the use-case you are trying, but the error you are getting is because your are trying to pass an object as a template type to po::value<>. If the size is known at compile-time, you could have the size be passed in as a template parameter.
template< typename T, size_t size >
class MyClass {
T m_data[size];
public:
// ...
};
And then use it like so:
po::value< MyClass<int, 2> >()
You should also look into using Boost.Array instead that I guess fulfills what you are trying to implement.
I would write it like this:
MyClass<int> mine(2);
generic.add_options() ("myclass", po::value(&mine), "Read MyClass");
Then all that needs to be done is to define an input stream operator like this:
std::istream& operator >>(std::istream& source, MyClass& target);
Then Boost Program Options will invoke this stream operator when the myclass option is used, and your object will be automatically populated according to that operator's implementation, rather than having to later call one of the Program Options functions to extract the value.
If you don't prefer the above syntax, something like should work too:
generic.add_options() ("myclass", po::value<MyClass<int> >()->default_value(MyClass<int>(2)), "Read MyClass");
This way you would be creating the instance of your class directly with your desired constructor argument outside of the template part where runtime behavior isn't allowed. I do not prefer this way because it's verbose and you end up needing to call more functions later to convert the value.