I have got a vector of objects vectors named "circuito"(circuit) in a "factory" class named "FactoryCircuit".
vector< vector< Elemento*> >
"Elemento" is a base class of three derived class named "resistenza,induttanza,conduttanza"(resistance,inductance,conductance). These three classes are the RCL components. (In this programme I must calculate I and Z.)
In every "place" of the INNER vector I can push_back only same components. And then I treat them as parallel components. The external vector rapresent place where the components are in series. So for example in the place [0] I can have one resistance, in [1] two inductance, in [2] four capacitance...
And if I try to copy that example I can do this in the main.cpp:
vector< vector<Elemento*> > circuito(3);
circuito[0].push_back(new Resistenza());
circuito[2].push_back(new Conduttanza());
circuito[2].push_back(new Conduttanza()); --->these "new" functions are three constructorS of the derived classes
circuito[2].push_back(new Conduttanza());
circuito[2].push_back(new Conduttanza());
circuito[1].push_back(new Induttanza());
circuito[1].push_back(new Induttanza());
circuito[0][0]->print();
circuito[0][1]->print(); //member function or R,C,L
circuito[0][2]->print();
This is correct but it crash when I run it. My goal is to create three member functions in the factory that create R,C,L but before doing this I must understand how to use vectors....
I however tried to create a member function in the factory named "CreaResistenza" for doing the same thing but it also crash:
Factory:
include ....etc....
FactoryCircuiti::FactoryCircuiti(){
vector< vector<Elemento*> > circuito(1);
}
FactoryCircuiti::~FactoryCircuiti(){
}
void FactoryCircuiti::CreaResistenza(double a) {circuito[0].push_back( new Resistenza(a) );}
//this last lines is the guilty! it causes the crash of the programme when in the main I use this member function!
Instead if I put in the constructor"
circuito[0].push_back(new Induttanza());
circuito[1].push_back(new Resistenza());
circuito[0][0]=(new Conduttanza());
circuito[0][0]->print();"
this is ok when I create the class... I did a lot of tries for understanding where is the problem but now I don't know what do :( Thanks for the response!
Your example :
vector< vector<Elemento*> > circuito(3);
circuito[0].push_back(new Resistenza());
circuito[2].push_back(new Conduttanza());
circuito[2].push_back(new Conduttanza()); --->these "new" functions are three constructorS of the derived classes
circuito[2].push_back(new Conduttanza());
circuito[2].push_back(new Conduttanza());
circuito[1].push_back(new Induttanza());
circuito[1].push_back(new Induttanza());
circuito[0][0]->print();
circuito[0][1]->print(); //member function or R,C,L
circuito[0][2]->print();
It creates a vector of 3 vectors, with data that I will represent that way :
circuito {
[0] : {
[0] Resistenza
}
[1] : {
[0] Induttanza
[1] Induttanza
}
[2] : {
[0] Conduttanza
[1] Conduttanza
[2] Conduttanza
[3] Conduttanza
}
}
And then, you want to print the :
circuito[0][0]->print();
OK, it exists
circuito[0][1]->print();
PROBLEM, does not exists, pointer is NULL : segmentation fault
You have to initialize your data first or to check if they are not null before trying to call a method on them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.