简体   繁体   中英

C++ std::vector problems

I'm 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;
    }

void ForwardNetwork::reset() {
   std::cout<< "Network size#" << this->layers.size() << std::endl;
   int index = -1;
   for ( int i = 0; i< this->layers.size(); i++ ){
       cout << "Layer[" << i << "] address#" << layers[i] << endl;
       if(layers[i] != NULL){
         layers[i]->reset();
       }
       else cout << "Layer NULL";
  }
}
};

Second class:

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

Main function:

ForwardNetwork* network = new ForwardNetwork();
ForwardLayer* layer2= new ForwardLayer(2);
network->addLayer(layer2);
ForwardLayer* layer3 = new ForwardLayer(3);
network->addLayer(layer3);
ForwardLayer* layer1 = new ForwardLayer(1);
network->addLayer(layer1);
network->reset();
Train* train = new Backpropagation(network);

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?

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

There is no reason why you need a dynamic_cast inside of ForwardNetwork::getLayers unless you are doing some type of polymorphic casting and want to certify that the pointer type that is being cast from can be converted to a ForwardLayer* type. The cast simply shouldn't fail if you are assigning from one ForwardLayer pointer to another. The fact that you're getting NULL pointers means that the cast is failing, and I suspect the issue is coming from the difference between a FeedforwardLayer and a ForwardLayer , but there is not enough code presented here to decipher how your inheritance hierarchy is constructed.

That being said, your NULL pointers are not a result of incorrectly adding pointers to your linked list, but instead are resulting from a failed dynamic_cast .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM