1

I am calling a function in a loop which takes argument as structure pointer (st *ptr) and i need to push_back this data to a STL vector and display the content in a loop.How can i do it? please help.

struct st
{
    int a;
    char c;
};
typedef struct st st;


function(st *ptr)
{
    vector<st*>myvector;
    vector<st*>:: iterator it;
    myvector.push_back(ptr);
    it=myvector.begin();
    cout<<(*it)->a<<(*it)->c<<endl;
}

is this correct? i am not getting the actual output.

Code snippet-----

void Temperature_sensor::temp_notification()//calling  thread in a class------
{

    cout<<"Creating thread to read the temperature"<<endl;
    pthread_create(&p1,NULL,notifyObserver_1,(void*)(this));
    pthread_create(&p2,NULL,notifyObserver_2,(void*)(this));
    pthread_join(p1,NULL);
    pthread_join(p2,NULL);

}


void* Temperature_sensor::notifyObserver_1(void *data)
{

    Temperature_sensor *temp_obj=static_cast<Temperature_sensor *>(data);   
    (temp_obj)->it=(temp_obj)->observers.begin();
    ifstream inputfile("temp.txt");//Reading a text file 

    while(getline(inputfile,(temp_obj)->line))
    {
        stringstream linestream((temp_obj)->line);
        getline(linestream,(temp_obj)->temperature,':');
        getline(linestream,(temp_obj)->temp_type,':');
        cout<<(temp_obj)->temperature<<"---"<<(temp_obj)->temp_type<<endl;
        stringstream ss((temp_obj)->temperature);
        stringstream sb((temp_obj)->temp_type);
        sb>>(temp_obj)->c_type;
        ss>>(temp_obj)->f_temp;
        cout<<"____"<<(temp_obj)->f_temp<<endl;
        (temp_obj)->a.temp=(temp_obj)->f_temp;
        (temp_obj)->a.type=(temp_obj)->c_type;
        cout<<"------------------q"<<(temp_obj)->a.type<<endl;
        (*(temp_obj)->it)->update(&(temp_obj)->a);//Calling the function -------

}
input file temp.txt
20:F
30:C
40:c
etc
void Temperature_monitor::update(st *p) {}//need to store in a vector------
smac89
  • 39,374
  • 15
  • 132
  • 179
Deepak
  • 47
  • 5
  • 1
    ... By pretty much putting into code exactly what you described? What didn't work? – Quentin Nov 08 '17 at 17:33
  • 1
    Do you have tried to compile? Result? – Klaus Nov 08 '17 at 17:42
  • If you are not getting the actual output, what *are* you getting? Also, C++ is not C; you don’t need the `typedef` line. And although there are uses for putting pointers in vectors like this, I suspect you might actually want to just put the `st`s directly inside the vector. – Daniel H Nov 08 '17 at 17:44
  • @Klaus this is part of my c++ code which i am doing and ever after displaying the cout as above i am not getting any output – Deepak Nov 08 '17 at 17:51
  • @DanielH is it not good practice to typedef in c++ code ? please suggest. – Deepak Nov 08 '17 at 17:53
  • @DanielH i am passing the stricture to the function and was receiving through structure pointer i tried the other way also,its not working – Deepak Nov 08 '17 at 17:55
  • What output are you getting? What compiler errors are you getting? These are questions you need to answer in order to get to a reasonable answer with your question. If the code you have above is exactly what you are trying to compile, then I'm afraid you have bigger problems; `function` is not a valid C++ function as it does not have a type. – smac89 Nov 08 '17 at 18:03
  • @smac89 updated the code. please see the code snippet – Deepak Nov 08 '17 at 18:09
  • You just need to declare the vector outside the function that uses it. Maybe as a member function of the `Temperature_sensor` class? This depends on how you want to use the vector of course. If both observers will be writing to this function, then it's ok to declare it inside the `Temperature_sensor` class, otherwise the `Temperature_monitor` class may be a better option – smac89 Nov 08 '17 at 18:29

3 Answers3

0

If you use a std::vector you should do something like this:

std::vector<st> v; //use st as type of v

//read
for(auto const& i : v) {
    std::cout << i.param1 << ' ' << i.param2;
}
//push_back
v.push_back({param1, param2});

Of course, you could have more than 2 params.

santo
  • 418
  • 1
  • 3
  • 13
0

Could you please share sample input data and expected output? With your code it will always create a new vector and put 1 structure object there. if you want to have single vector store all structure objects, then declare vector in calling function of "function"

Rajeev Ranjan
  • 23
  • 1
  • 4
  • You can make this into an answer now that OP has updated his post. You are on the right track, you just need to flesh out the answer a bit – smac89 Nov 08 '17 at 18:27
0

It looks like you're allocating a buffer data of type void* with malloc() or a similar function, then casting data to Temperature_sensor*. It also appears that Temperature_sensor is a class with std::string members, which you are attempting to assign to and print.

This will not work because std::string is not a POD type, and so the std::string constructor is never actually invoked (likewise, Temperature_sensor is not a POD type because it has non-POD members, and its constructor is therefore never invoked).

To construct the objects correctly you need to use operator new() in place of malloc() like so

Temperature_sensor *tsensor = new Temperature_sensor;
Temperature_sensor *five_tsensors = new Temperature_sensor[5];

It would be more idiomatic to use a smart pointer like std::unique_ptr or std::shared_ptr instead of using operator new() (and operator delete()) directly, and best/most idiomatic to use a std::vector. Any of these methods will construct the allocated objects correctly.

You should also strongly consider dramatically simplifying the Temperature_sensor class. It appears to have numerous instance variables that redundantly store the same information in different formats, and which would make more sense as local variables inside your functions.

You also don't need to be creating all those std::stringstream's; consider using std::stod() and std::stoi() to convert strings to floating-point or integers, and std::to_string() to convert numbers to strings.

Ray Hamel
  • 1,289
  • 6
  • 16