pointers to vectors as class members pointers to vectors in functions - class

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

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

The rule of The Big Three

Iam confused with the below question I did the program as per my understanding but it crashes what am I doing wrong? If someone can please assist me it would be much appreciated.
my main.cpp looks like this:
#include <iostream>
#include <iomanip>
#include "Number.h"
using namespace std;
int main()
{
Number n1(10);
Number n2 = n1;
n2.printNum();
n2.addOne();
n1 = n2;
n1.printNum();
return 0;
}
Then my header file looks like this:
#include <iostream>
using namespace std;
class Number
{
int *p;
public:
Number(int);
void addOne();
void printNum();
};
And the below parts for the constructor I need to complete there where it shows comments that's the part I should complete:
#include <iostream>
#include "Number.h"
using namespace std;
Number::Number(int a1)
{
*p = a1;//write the code needed to initialise the value of the member variable with a1
}
void Number::printNum()
{
cout << "The number is " << *p << endl;
}
void Number::addOne()
{
*p++;//write the code needed to increment the value of the member variable by one.
}
Then the question asks the below what should I do to the code to use the BIG THREE?
Consider the following program. Complete the class definition (where you are asked to) and check the output. You can see that that program works without error once it is completed. However, experts suggest that in any class that uses pointers and the new operator it is better to follow the rule of The Big Three. Modify the class definition to follow the rule of The Big Three and submit the new program and the output. Demonstrate the use of this pointer.
Thank you
Rohan

cannot sort a vector of objects that has array in it

Hi I am using a class that has an integer and array and creating a vector of the classes objects but I cannot sort it also don't know to store in it.
I am a BEGINNER on c++ so i just wanted to know if I am wrong and how to
do that thing
here n = no of times the program has to execute
num = to store the no. of elements in vector a
but problem loop for(j=0;j<arr[i].a.end();j++)
and also pushback is not working
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class subcont
{
public:
int num;
vector<int> a;
};
int main()
{
vector<subcont> arr(100);
int i,j,k,l,n,num1,num2;
cin>>n;
for(i=0;i<n;i++)
{
cin>>arr[i].num;
for(j=0;j<arr[i].num;j++)
{
cin>>num2;
cin>>arr[i].a.pushback(num2);
}
}
for(i=0;i<n;i++)
{
sort(arr[i].a.begin(),arr[i].a.end());
}
for(i=0;i<n;i++)
{
cout<<arr[i].num;
for(j=0;j<arr[i].a.end();j++)
cout<<arr[i].a[j];
}
return 0;
}
The problems you describe sound as if you could at least compile your code, which I can't. In fact, the compiler error messages (if one first ignores the large amount of error noise generated by unhappy templates) should hint to most important problems.
Logical problems on first sight: In for(j=0;j<k;j++) the value of k is undefined. In for(j=0;j<arr[i].a.end();a++) the a++ does not make sense.
In cin>>arr[i].a.pushback[num]; the num should probably be num2. Please check your code for more such typoes.
Your sort fails because a is a C array and not a C++ container, so a.begin() and a.end() are not defined.
Stylistic problem: While it makes life a lot easier, mayn people strictly recommend to not use using namespace std;
Additional remark: Why not use std::vector<int> in place of subcont?

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)