0

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!

Keys88
  • 1
  • can you show more of your class for factoryCircuiti? At first glance, it looks like you're constuctor isn't initializing a member but hiding it with a local variable. (try changing your constructor to `FactoryCircuiti::FactoryCircuiti() : ciruito(1){}` – IdeaHat Jan 27 '14 at 17:34
  • This class has got other two variables that can be modified with two member functions. But the problem is not here, because when I built the factory I followed this path: 1)factory and two variables and constructor and destructor. It's ok 2) build member functions. It's ok 3) build vectors and "CreaResistenza", etc...it's ok... but not when I use the main... :P thanks for help! – Keys88 Jan 28 '14 at 13:16

1 Answers1

0

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.

dkg
  • 1,775
  • 14
  • 34
  • How could I fix the problem directly in the member function? Or the problem is not in the member function but in the main? – Keys88 Jan 28 '14 at 13:11
  • Yeah, you create a vector of size 3 containing 3 other vectors containing nothing. The size of the other vectors will grow when adding an element, then if you add one element to a vector, only the first is accessible, and then you try to access the second : there is the problem. – dkg Jan 30 '14 at 09:28
  • By the way, you can use the method vector<>::size() to know how many elements are in your vector. In your case, circuito[0].size() = 1; circuito[1].size() = 2; circuito[2].size() = 4; – dkg Jan 30 '14 at 09:44