Class with 'operator=' got compiler error when using erase() in vector [on hold]

General Tech Bugs & Fixes 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I implement an operator= function in a class, and using vector to store a sequence of objects.

Compiler error arises when I try to erase an object of the vector.

class A{
    A();
    void operator=(const A &x){
    }
}
int main(){
    std::vector<class A>vectorA;

    for(int i = 0; i<10; i++){
        A tempA;
        vectorA.push_back(tempA);
    }

    vectorA.erase(vectorA.begin()+2);
}

And here is the detail of the compiler error.

/Users/meow/Desktop/DP/Code/functionTest.cpp:101:21: required from here /usr/local/Cellar/gcc@7/7.4.0/include/c++/7.4.0/bits/stl_algobase.h:343:18: error: cannot bind non-const lvalue reference of type 'A&' to an rvalue of type 'std::remove_reference::type {aka A}'__result = std::move(__first);

Any help is appreciated.

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.