Add pointer to a std :: vector

0

Hi, I need to add in a vector<TimedCommand> tCommands; access references for TimedCommand instances, eg:

    TimedCommand command1("*.*.*.*.*.*",&sonCommand,"13"); 
    (...)
    tCommands.push_back(&command1);

Unfortunately, when I do this happens a no matching function for call to 'std::vector<TimedCommand', std::allocator<TimedCommand>>:: push_back(TimedCommand*)

I'm using the Standard C ++ for Arduino lib and Arduino Cron Library . What am I doing wrong?

    
asked by anonymous 05.11.2014 / 14:58

1 answer

1

You are trying to add a pointer to a std::vector of "normal" objects.

std::vector<TimedCommand> tCommands;
TimedCommand command1(...);
tCommands.push_back( &command1 ); //Erro de compilação

The last line attempts to convert a TimedCommand* to TimedCommand , which is invalid.

You have two options: change your std::vector to store pointers

std::vector<TimedCommand*> tCommands;
TimedCommand *command1 = new TimedCommand(...); //Alocação dinâmica
tCommands.push_back( command1 ); 

This involves manual memory management, or adding a copy of your variable to std::vector :

std::vector<TimedCommand> tCommands;
TimedCommand command1(...);
tCommands.push_back( command1 ); 

What may invalidate some logic of your algorithm, or not.

    
06.11.2014 / 13:22