First of all, I'm relatively new to C and C++, I'm way more used to C#.
Basically, I have a vector an object called CTe, this object has several variables and a vector of another object called NFe, all the information that I want to fill the CTes objects are stored in a 2D vector of strings, so the goal of this loop is to iterate through all the lines of the vector, being one CTe per line, and this CTe may have one or multiples NFes attached to it.
/*The declaration of the vectors, just to help visualize the structure
vector < string > dirlist;
vector < vector < string > > listElement;
vector < CTe > ctes;*/
//This will iterate through the 2D vector
for (int i = 1; i < listElement.size(); i++)
{
//Temporally CTe object to store information
CTe temp;
string targetCNTR = listElement[i][6];
int countNFE = 0;
bool haveFoundReference = false;
// This will iterate reading all the xmls I have in a folder
for (int j = 2; j < dirlist.size(); j++)
{
string extraInfoNFE = ReadXML(dirlist[j], "infAdic", "infCpl");
//Check if a valid xml was found.
if (extraInfoNFE.find(targetCNTR) != string::npos)
{
haveFoundReference = true;
string sColeta = "000"+listElement[i][3];
stringstream tipoNumber (listElement[i][4]);
//Fill the variables of the temp object
temp.cntr = targetCNTR.c_str();
temp.booking = listElement[i][0].c_str();
temp.motorista = listElement[i][1].c_str();
temp.placas = listElement[i][2].c_str();
temp.coleta = sColeta.c_str();
temp.seqEndereco = listElement[i][5].c_str();
tipoNumber >> temp.tipoCTE;
listElement[i][8+countNFE] = ReadXML(dirlist[j], "ide", "nNF");
//Create a temporally object to store the NFe information
NFe tempNFe;
//Fill the tempNFe object
stringstream nfeNumber (listElement[i][8+countNFE]);
nfeNumber >> tempNFe.numeroNFE;
string sXML = dirlist[j].substr(5, 43);
tempNFe.codigoXML = sXML.c_str();
string sDest = ReadXML(dirlist[j], "dest", "xNome");
tempNFe.destinatario = sDest.c_str();
stringstream cfopNumber (ReadXML(dirlist[j], "det", "prod", "CFOP"));
cfopNumber >> tempNFe.cfop;
stringstream qtdeNumber (ReadXML(dirlist[j], "transp", "vol", "qVol"));
qtdeNumber >> tempNFe.qtde;
stringstream valorNumber (ReadXML(dirlist[j], "total", "ICMSTot", "vNF"));
valorNumber >> tempNFe.valor;
stringstream pesoNumber (ReadXML(dirlist[j], "transp", "vol", "pesoB"));
pesoNumber >> tempNFe.pesoBruto;
//push_back the tempNFe into the temp object
//This part is working perfectly
temp.notas.push_back(tempNFe);
countNFE++;
}
}
//Check if a valid xml was found, if so push_back
//The temp object into the ctes object
//HERE LIES THE PROBLEM
if (haveFoundReference)
{
cout<<temp.cntr<<" - ";
ctes.push_back(temp);
}
else
{
cout << "Não foi possível localizar a nota do CNTR " <<targetCNTR;
}
}
The problem is, that all the CTe objects in the ctes vector are the same, the only thing that is working is the NFe vector inside the CTe.
Here is the CTe and the NFe classes:
NFE
class NFe
{
public:
NFe();
const char* codigoXML;
const char* destinatario;
int numeroNFE;
int cfop;
int qtde;
float valor;
float pesoBruto;
~NFe();
};
CTE
#include <nfe.h>
#include <vector>
class CTe
{
public:
CTe();
const char* motorista;
const char* placas;
const char* booking;
const char* cntr;
const char* seqEndereco;
const char* coleta;
int espelho;
int tipoCTE;
std::vector < NFe > notas;
virtual ~CTe();
};