C++ std::vector problems

Posted by Faur Ioan-Aurel on Stack Overflow See other posts from Stack Overflow or by Faur Ioan-Aurel
Published on 2012-03-19T17:33:44Z Indexed on 2012/03/19 18:04 UTC
Read the original article Hit count: 309

Filed under:
|
|
|
|

For 2 days i tried to explain myself some of the things that are happening in my c++ code,and i can't get a good explanation.I must say that i'm more a java programmer.Long time i used quite a bit the C language but i guess Java erased those skills and now i'm hitting a wall trying to port a few classes from java to c++.

So let's say that we have this 2 classes:

class ForwardNetwork {
protected:
    ForwardLayer* inputLayer;
    ForwardLayer* outputLayer;
    vector<ForwardLayer* > layers;
public:
    void ForwardNetwork::getLayers(std::vector< ForwardLayer* >& result ) {
        for(int i= 0 ;i< layers.size(); i++){
            ForwardLayer* lay = dynamic_cast<ForwardLayer*>(this->layers.at(i));
            if(lay != NULL)
                result.push_back(lay);
            else cout << "Layer at#" << i << " is null" << endl;
        }

    }
    void ForwardNetwork::addLayer ( ForwardLayer* layer ) {
        if(layer != NULL)
            cout << "Before push layer is not null" << endl;
        //setup the forward and back pointer
        if ( this->outputLayer != NULL ) {
            layer->setPrevious ( this->outputLayer );
            this->outputLayer->setNext ( layer );
        }
        //update the input layer and outputLayer variables
        if ( this->layers.size() == 0 )
            this->inputLayer = this->outputLayer = layer;
        else this->outputLayer  = layer;

        //push layer in vector
        this->layers.push_back ( layer );

        for(int i = 0; i< layers.size();i++)
            if(layers[i] != NULL)
                cout << "Check::Layer[" << i << "] is not null!" << endl;
    }
};

Second class:

class Backpropagation : public Train {
public:
    Backpropagation::Backpropagation ( FeedForwardNetwork* network ){
        this->network = network;
        vector<FeedforwardLayer*> vec;
        network->getLayers(vec);
    }
};

Now if i add from main() some layers into network via addLayer(..) method it's all good.My vector is just as it should.But after i call Backpropagation() constructor with a network object ,when i enter getLayers(), some of my objects from vector have their address set to NULL(they are randomly chosen:for example if i run my app once with 3 layer's into vector ,the first object from vector is null.If i run it second time first 2 objects are null,third time just first object null and so on). Now i can't explain why this is happening.I must say that all the objects that should be in vector they also live inside the network and they are not NULL;

This happens everywhere after i done with addLayer() so not just in the getLayers(). I cant get a good grasp to this problem.I thought first that i might modify my vector.But i can't find such thing. Also why if the reference from vector is NULL ,the reference that lives inside ForwardNetwork as a linked list (inputLayer and outputLayer) is not NULL?

I must ask for your help.Please ,if you have some advices don't hesitate!

PS: as compiler i use g++ part of gcc 4.6.1 under ubuntu 11.10

© Stack Overflow or respective owner

Related posts about c++

Related posts about pointers