user2212461
user2212461

Reputation: 3253

C++: Pass with "typedef" defined class as parameter

I am creating an instance Dataset::Ptr data and then I need to pass this instance to another method, but I am having trouble with passing "data" as an argument.

The Dataset class is defined as follows:

//Definition of "Dataset" class in Datset.h
class Dataset : public Objects
{
public:
    typedef boost::shared_ptr<Dataset> Ptr;

...

        void foo(); 

In the class where I instanciate a "Dataset" object I have:

void doWork(Dataset::Ptr* ds)
{
ds->foo();------>Here I get the error that foo is not defined. ds doesnt have any 
}

void Function(){
Dataset::Ptr* ds;
....do something with ds....
doWork(&ds);
}

The error is that boost::shared_ptr cannot be changed into Dataset::Ptr.

What am I doing wrong? How can I pass the instance properly?

Thanks

Upvotes: 1

Views: 300

Answers (3)

Mike Seymour
Mike Seymour

Reputation: 254501

For some reason, doWork takes a pointer to a (shared) pointer. So ds->foo() tries to call a foo member of the shared pointer, not the object that points to.

You probably want to pass the shared pointer by value or const reference:

void doWork(Dataset::Ptr ds);
doWork(ds);

But if your pointer dance is necessary for some reason, then you'll need to dereference twice:

(*ds)->foo();

Upvotes: 0

westwood
westwood

Reputation: 1774

Dataset::Ptr* means pointer to boost::shared_ptr<Dataset>.

Upvotes: 0

billz
billz

Reputation: 45410

Dataset::Ptr is a pointer already, Dataset::Ptr* means boost::shared_ptr<Dataset>* Ptr; ds becomes a pointer to boost::shared_ptr which has no foo() function defined.

update

void doWork(Dataset::Ptr* ds)

to

void doWork(Dataset::Ptr ds)

Upvotes: 2

Related Questions