vendredi 25 décembre 2015

Converting shared_ptr to shared_ptr of same type in different namespace

I have this Foo class where it contains a shared_ptr to a Hotel class, and a reference to a Rules class (which is inside a namespace Rules):

class Foo
{
public:
   //...
   void doStuff();
private:
   std::shared_ptr<Hotel> mHotelClicked;
   Rules::IRules& mRules;
}

Where the doStuff() method is implemented in this way:

void Foo::doStuff()
{
   //...
   std::shared_ptr<Hotel> p = hotel;

   //Here I need to pass both smart pointers
   if(mRules.isValidMove(mHotelClicked,p) == true) //ERROR here! See bellow.
   {
       //...
   }
}

The Rules is inside a namespace called Rules in the following interface:

namespace Rules
{
  class IRules
  {
  public:
    virtual bool isValidMove(std::shared_ptr<Hotel> hotel1, std::shared_ptr<Hotel> hotel2) = 0;
    //...
  };
}

ERROR:

error C2664: 'Rules::IRules::isValidMove' : cannot convert parameter 1 from 'std::shared_ptr<_Ty>' to 'std::shared_ptr<_Ty>'

When I hover the mouse over the:

mRules.isValidMove(mHotelClicked,p)

I see the following ERROR:

No suitable user-defined conversion from "std::shared_ptr<Hotel>" to "std::shared_ptr<Rules::Hotel>" exits.

Notice the Rules:: (maybe because it's from the namespace).

My two questions:

  • [1] How can I fix this error? Since both parameters are the same type? Both are smart pointers to a Hotel class.

  • [2] What is the best practice to do this? Should I pass by reference instead?

Aucun commentaire:

Enregistrer un commentaire