0

Assume for a second we have

#include <boost/shared_ptr.hpp>
#include <iostream>
int main()
{
   boost::shared_ptr<int> bleah(); //default constructor
   cout << bleah.get() << endl; //error line

   boost::shared_ptr<int> barf(new int (10));
   cout << *barf.get() << endl; //outputs 10 as normal.


}

How come this does not compile? It's as if the function shared_ptr::get just all of a sudden disappeared from the class definition for the variable "bleah". I'm sure there's a reason, but I cannot see it at the moment.

sbrett
  • 606
  • 4
  • 20

2 Answers2

7

The first isn't a shared_ptr, it's a function taking zero arguments that returns a shared_ptr with name bleah, remove the ().

Ylisar
  • 4,293
  • 21
  • 27
3

This is called the Most vexing Parse in C++.

With this statement:

boost::shared_ptr<int> bleah(); //default constructor

You declared a function taking zero arguments that returns a shared_ptr with name bleah.

To create an object, change it to:

boost::shared_ptr<int> bleah;
Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533