Good morning everyone, I currently have an issue in my code, it crashes without any error output. I was hopping you could lend me a hand!
The issue occurs when I append an element at a vector, but not at the first iteration always at the second.
Here is my main code:
/*Various includes*/
int main(void)
{
int cmpt = 0;
std::vector<Commande> _vct;
Commande cmd;
while(cmpt < 15)
{
cout << "..." << endl;
_vct.push_back(cmd);
cout <<" test cmd" << _vct[cmpt].getBytes() << endl;
cmpt++;
}
return 0;
}
Here is the object's class that is appened to the vector:
Commande::Commande()
{
bytes="";
from="";
type="";
first_s="";
last_s="";
value=-1;
first_i=-1;
last_i=-1;
}
Commande::~Commande()
{
}
void Commande::toString()
{
cout << "=-_-=> from: '" << (this)->getFrom() << "', type: '" << (this)->getType() << "', value: '" << (this)->getValue() << "', bytes: '" << (this)->getBytes() << "'." << endl;
}
void Commande::fromStringToInteger()
{
(this)->setFirst_i(atoi((this)->getFirst().c_str()));
(this)->setLast_i(atoi((this)->getLast().c_str()));
}
And finally the .h of Commande.cpp:
#include <cstdlib>
#include <iostream>
#ifndef COMMANDE_H_
#define COMMANDE_H_
using namespace std;
class Commande
{
public:
Commande();
virtual ~Commande();
/*Various geters ans seters*/
void toString(void);
void fromStringToInteger();
private:
string bytes, from, type, first_s, last_s;
int value, first_i, last_i;
};
#endif /* COMMANDE_H_ */
Thank you all for your time!
Unrelated to the problem you ask about, but there are many other problems...
For example lets take a closer looks at these lines (excerpt from yout main
function):
while(...)
{
Commande* cmd = new Commande();
(this)->appendCmdList(*cmd);
}
First of all it should not compile. There's no this
in non-member functions, only in non-static member functions.
Then you have the memory leak in that you allocate a new Commande
instance dynamically, but you pass the instance (and not the pointer) to the appendCmdList
function, which means the next time the loop iterates the pointer is lost (as it goes out of scope) and you allocate a new pointer without deleting the previous object, or saving the pointer.
Without a Minimal, Complete, and Verifiable Example it's impossible to say anything else.