0

I am currently reading Accelerated C++ ch13 and thought of doing sample program given in book via boost scoped_ptr but have encountered an error.
May you guys please bail me out.

**

***error: cannot use arrow operator on a type
      record->read( cin );***
            ^

**

Original sample code is something shown below and this works flawlessly

std::vector< Core* > students ; // read students
    Core* records;
    std::string::size_type maxlen = 0;
    // read data and store in an object
    char ch ;

        while( cin >> ch )
        {
            if( 'U' == ch )
            {
                records = new Core;

            }
            else if( 'G' == ch )
            {
            records = new Grad;
            }

         records->read( cin );

        maxlen = max( maxlen , records->getname().size() );
        students.push_back( records );
    }

Now using scoped_ptr MY VERSION

typedef boost::scoped_ptr<Core> record;
    std::vector< record > students;
    char ch;
    std::string::size_type maxlen = 0;
    // read and store
    while( ( cin >> ch )   )
    {
        if( ch == 'U')
        {
            record( new Core);
        }
        else if( ch == 'G')
        {        
             record( new Grad);
        }

       record->read( cin );// GOT ERROR
       //maxlen = max( maxlen, record->name().size() );// SAME type of  error I EXPECT HERE
      // students.push_back( record );

    }
samprat
  • 2,150
  • 8
  • 39
  • 73
  • 2
    `record` is a type, not a variable. – Igor Tandetnik Apr 05 '15 at 02:22
  • @IgorTandetnik, Thanks mate , but when i use boost::scoped_ptr record without typedef it throws different error. So what is the corrrect way to solve it – samprat Apr 05 '15 at 02:25
  • `boost::scoped_ptr record; if (ch == 'U') record.reset(new Core); else record.reset(new Grad); record->read(cin);` – Igor Tandetnik Apr 05 '15 at 02:27
  • I did what you said. in addition std::vector< boost::scoped_ptr > students;// is there better way to do this line? Also students.push_back( record ); throws error ->/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/4.2.1/ext/new_allocator.h:107: error: calling a private constructor of class 'boost::scoped_ptr' { ::new(__p) _Tp(__val); } – samprat Apr 05 '15 at 02:38
  • 2
    I'm pretty sure `boost::scoped_ptr` can't be stored in a `vector` - it's not copyable and not movable. Use `std::unique_ptr` instead. – Igor Tandetnik Apr 05 '15 at 02:47
  • @IgorTandetnik, Thanks mate , I will try that and let you know how i did. Thanks again for prompt help – samprat Apr 06 '15 at 02:30

0 Answers0