5 years, 7 months ago.

How to use struct value in vector using classes

Hello, I need some help. if i have two classes class A and class B. Class A has a structure "input". for example

struct input
{

double kp ;

}

Class A
{
input *obj;
public:

A(input*)

}

now in A.cpp if i assign a value to kp then

this->obj->kp =0.3000;

and after that i want to use kp value in class B

now i creat B.h lets say in same namespace as i used in A, and in B.cpp i print out the value of kp it will come, but when i creat a vector of double and push_back kp then i don't get it value.

for example

    vector<double> *control;

   this->control->push_back(this->obj->kp);

   printf("the value of kp is %lf= \n", this->control[0]);  // and it gives 0.000000

but if i print out

printf("the value of kp is %lf= \n", this->obj->kp);                 // i will get the right value 0.30000

Can anyone please tell me my mistake. Thank you very much

2 Answers

5 years, 7 months ago.

Hello Ali,

  • It isn't quite clear for me where is class B defined and how can be a private member of class A (like obj) used within class B?
  • As Andy pointed out, control in your code is an uninitialized pointer pointing to some random location in the memory (there is no object of vector<double> type created in your code snippet to point to by control).
  • Since control is a pointer I do not understand how could an expression like this->control[0] compile? The control pointer must be dereferenced before using it, like (*this->control)[0].

A few advices:

  • To have a more readable code published here (on mbed pages) try to mark it up as below (with <<code>> and <</code>> tags, each on separate line at the begin and end of your code)
<<code>>
text of your code
<</code>>
  • All the containers in the standard library hold copies of the objects you place in them so your objects must be copy-constructible (have an accessible copy constructor) and assignable (have an accessible assignment operator).
  • You can ommit the this pointer inside a class definition, unless you need it to resolve an ambiguous situation.

An example which might be of help:

#include <iostream>
#include <vector>

using namespace std;

struct input
{
    double  kp;
};

class   A
{
public:
    input*              obj;
    vector<double>*     control;

    A(input* in, vector<double>* cont) : obj(in), control(cont) {
        obj->kp = 0.3;
        control->push_back(obj->kp);
        cout << "object of class A: (*control)[0] = " << (*control)[0] << endl;
    }
};

class   B
{
public:
    vector<double>*     control;

    B(vector<double>* cont) : control(cont) { 
        cout << "object of class B: (*control)[0] = " << (*control)[0] << endl;
    }
};

int main()
{
    input           in;
    vector<double>  ctrl;
    A               a(&in, &ctrl);
    B               b(&ctrl);

    cout << "a.obj->kp       = " << a.obj->kp << endl;
    cout << "(*a.control)[0] = " << (*a.control)[0] << endl;
    cout << "(*b.control)[0] = " << (*b.control)[0] << endl;

    return 0;
}

Below is the ouput:

object of class A: (*control)[0] = 0.3
object of class B: (*control)[0] = 0.3
a.obj->kp = 0.3
(*a.control)[0] = 0.3
(*b.control)[0] = 0.3

Accepted Answer
5 years, 7 months ago.

vector<double> *control;
this->control->push_back(this->obj->kp);

doesn't create a vector at any point, only a pointer to a vector. Try something more like

vector<double> control;
this->control.push_back(this->obj->kp);

Hi Andy

Thanks for your reply, but i think this makes no difference.

because if i write a value like

this->control->push_back(0.3000); it works fine

but if i used a struct value then it will show 0.00 in this->control[0]

posted by Ali Imran Siddiqui 06 Sep 2018