c++ - Looping through a unique_ptr collection outside of an object -


i'm trying loop through collection of pointers inside object baz outside class having class return vector::iterator. when run for loop following error:

'const class std::unique_ptr' has no member named 'getname'

can explain what's going on , how can manage loop through unique pointer collection inside baz? in advance.

#include <iostream> #include <string> #include <memory> #include <vector>  class baseinterface { protected:  std::string name;  public:  virtual ~baseinterface(){} virtual void setname( std::string objname ) = 0; virtual std::string getname() = 0; };  class foo : public baseinterface { public:  void setname( std::string objname ) {     name = objname; }  std::string getname() {     return name; }  };  class bar : public baseinterface { public:  void setname( std::string objname ) {     name = objname; }  std::string getname() {     return name; }  };  class baz {     protected:      std::vector< std::unique_ptr< baseinterface > > pointerlist;      public:      void push_back( std::unique_ptr< baseinterface > object )     {         pointerlist.push_back( std::move( object ) );     }      std::vector< std::unique_ptr< baseinterface > >::const_iterator begin()     {         return pointerlist.begin();     }      std::vector< std::unique_ptr< baseinterface > >::const_iterator end()     {         return pointerlist.end();     }  };  int main( int argc, char ** argv ) {  std::unique_ptr< baseinterface > fooobj( new foo() ); fooobj->setname( "foo" );  std::unique_ptr< baseinterface > barobj( new bar() ); barobj->setname( "bar" );  std::unique_ptr< baz > bazobj( new baz() );  bazobj->push_back( std::move( fooobj ) ); bazobj->push_back( std::move( barobj ) );  for( auto = bazobj->begin(); != bazobj->end(); ++it ) {     std::cout << "this object's name " << it->getname() << std::endl; }  return 0; } 

you should first dereference iterator:

std::cout << "this object's name " << (*it)->getname() << std::endl; //                                       ^^^^^ 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -