Note that there are some explanatory texts on larger screens.

plurals
  1. POSet of shared_ptr and set function count
    primarykey
    data
    text
    <p>I have a set of shared pointers.</p> <p>I want to make a function that checks if a shared pointer exists in it.</p> <p>I assumed I need to use set.count to get it.</p> <p>But I get 0 all the time... even though the shared pointer exists in there.</p> <p>So my question is: how does count compare shared pointers?</p> <p>Please bare in mind I am new to C++, I can provide with code, but there's a lot of it.</p> <p>EDIT 2: The function that checks the building and adds a car.</p> <pre><code>void Garage::addCar(Car&amp; n, Building&amp; m){ shared_ptr&lt;Building&gt; mm(new Building(m)); if (this-&gt;Buildings.count(mm) == 0){ cout &lt;&lt; m.name &lt;&lt; " doesn't exist in " &lt;&lt; this-&gt;name &lt;&lt; "." &lt;&lt; endl; return; } shared_ptr&lt;Car&gt; s(new Car(n)); m.addCar(s); Cars.insert(s); } </code></pre> <p>Function that creates Buildings</p> <pre><code> Building Garage::create Building(string name){ shared_ptr&lt; Building&gt; f_ptr(new Building(name)); Buildings.insert(f_ptr); return (*f_ptr); } </code></pre> <p>Class garage</p> <pre><code>class Garage { public: Garage(string name); ~Garage(); void admitCar(Car&amp; n, Building&amp; m); void dismissCar(Car&amp; n, Building&amp; m); void employEmployee(Employee&amp; n, Building&amp; m); void dismissEmployee(Employee&amp; n, Building&amp; m); Building createBuilding(string name); void deleteBuilding(Building&amp;); private: int nextIndex; string name; set&lt;shared_ptr&lt;Building&gt; &gt; Buildings; set&lt;shared_ptr&lt;Employee&gt; &gt; Employees; set&lt;shared_ptr&lt;Car&gt; &gt; Car; } </code></pre> <p>Whenever I want to add a car to a building, it says that that building doesnt exist in that garage...</p> <p>SOLUTION(my own):</p> <pre><code>bool Garage::hasBuilding(shared_ptr&lt;Building&gt; f){ return (this-&gt;Buildings.count(f) != 0); } </code></pre> <p>This is the function I created. It works.</p> <p>I tried Jonathan Wakely and LiKao ways but they were a bit too complicated for me and I had problems which again I couldn't myself solve.</p> <p>Thank You for the help :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload