0

i'm trying to work with a class that uses a vector of smartpointer with reference counting that it had to implement on my own. everything works fine but when i try to remove an iterator from my vector i get the following error: (inside algorithm) semantic issue no viable overloaded '=' which occurs at:

template <class _InputIterator, class _OutputIterator>
inline _LIBCPP_INLINE_VISIBILITY
_OutputIterator
__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
{
    for (; __first != __last; ++__first, ++__result)
        *__result = _VSTD::move(*__first);
    return __result;
}

which i guess is part of ...

here is my code:

#include "Project.h"
#include "Date.h"
#include "mSharedPtr.h"
class ProjectCycle
{
private:
    mSmartPtr<Project> project;
    Date startDate;
    int numberOfEmployees;
    std::vector<mSmartPtr<Employee>> employeeList;

public:
    ProjectCycle(const mSmartPtr<Project>& pro, const Date& strdate, const int& numemp, const std::vector<mSmartPtr<Employee>>& list) :
    project(pro), startDate(strdate), numberOfEmployees(numemp), employeeList(list) {};
    void finishCycle();
    ~ProjectCycle();

    void timeForwardHours(int);
    void startProject();
    void terminateProject();
    void removeEmp(emp_vec::iterator);
};

cppFile:

#include "ProjectCycle.h"
#include <vector>

void ProjectCycle::timeForwardHours(int num) {
    int totalDay=0;
    //   For each day ////////////////////////////////
    for (int day=0; day<num; totalDay=0,day++) {
        // For each employee ////////////////////////////
         emp_vec::iterator empIter = employeeList.begin();
        for (; empIter != employeeList.end(); empIter++) {
            totalDay += (*empIter)->addHoursToCurrProj();
            if ( (*empIter)->isDoneProject())
                removeEmp(empIter);
        }
        ///  Progress project ////////////////////////////////////
        project->setHoursLeft( project->getHoursLeft()-totalDay );
        if (project->getHoursLeft() <= 0) {
            terminateProject();
            break;
        }
    }
}
void ProjectCycle::removeEmp(emp_vec::iterator itr){
    project->addToDoneList(itr);
    employeeList.erase(itr);

thanks in advance,


itorra
  • 13
  • 4
  • possible duplicate of [Erasing elements from a vector](http://stackoverflow.com/questions/347441/erasing-elements-from-a-vector) – Cory Kramer Jun 03 '15 at 16:51
  • 2
    When you are iterating over a container using a `for` loop and call `erase`, you invalidate the iterator for the next iteration. See the above link for a solution to this, as it is a common issue. – Cory Kramer Jun 03 '15 at 16:52
  • Do you have a valid `operator=()` overload in `mSmartPtr`? – R Sahu Jun 03 '15 at 17:02
  • @CoryKramer: This sounds more like a compiler error, than a runtime error. – Benjamin Lindley Jun 03 '15 at 17:03
  • See the [rule of five](http://en.cppreference.com/w/cpp/language/rule_of_three). – Mark Ransom Jun 03 '15 at 17:05
  • Change `void ProjectCycle::removeEmp(emp_vec::iterator itr)` to `iterator ProjectCycle::removeEmp(emp_vec::iterator itr)` and follow the notes in http://stackoverflow.com/questions/4645705/vector-erase-iterator –  Jun 03 '15 at 17:06
  • @BenjaminLindley While some compilers may *warn* you about such behavior, it is not an *error*, it will compile just fine (unfortunately) – Cory Kramer Jun 03 '15 at 17:07
  • @CoryKramer: I'm talking about the OP's error. "semantic issue no viable overloaded '='". That sounds like a compiler error. Whereas the duplicate you linked is about a runtime issue. – Benjamin Lindley Jun 03 '15 at 17:08
  • Oh I see what you meant. Yes the OP has two issues. Their issue with the move semantics is causing their compile error, the linked issue is the other, you are correct. Perhaps they haven't even noticed the latter issue yet :) – Cory Kramer Jun 03 '15 at 17:09
  • hey everyone... i tried reading the material you supplied, unfortunately I still couldn't get the code to work. my operator= pf the smartPtr() template is valid. I used the return value of the erase() and still no luck. I also tried using std::remove(...) and got even more errors. I'm on the ledge ;( – itorra Jun 03 '15 at 18:11
  • http://stackoverflow.com/help/mcve – Benjamin Lindley Jun 03 '15 at 19:48

1 Answers1

0

I think the problem is caused by lack of a valid operator=() overload in mSmartPtr<Employee>.

Add the following member function

mSmartPtr& operator=( mSmartPtr&& rhs );

in mSmartPtr.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • operator=() is valid, and tested – itorra Jun 03 '15 at 18:16
  • @itorra, I assume you have a copy assignment operator (`mSmartPtr& operator=( mSmartPtr const& rhs );`). Do you have the move-assignment operator (`mSmartPtr& operator=( mSmartPtr&& rhs );`) also? – R Sahu Jun 03 '15 at 18:18