2012-05-13 2 views
0

내 벡터에서 '2'를 제거하기 위해이 지우기 기능을 실행하려고 할 때 오류 목록이 발생했습니다. 나는 문제가 어디 있는지 모르겠습니다. 도움말 크게 감사드립니다!벡터의 특정 값 제거

구조체 민트

struct MyInt 
{ 
friend ostream &operator<<(ostream &printout, const MyInt &Qn) 
{ 
    printout<< Qn.value << endl; 
    return printout; 
} 

    int value; 
    MyInt (int value) : value (value) {} 
}; 

struct MyStuff 
{ 
    std::vector<MyInt> values; 

    MyStuff() : values() 
    { } 
}; 

주요 구조체 mystuff에

int main() 
{ 
MyStuff mystuff1,mystuff2; 

for (int x = 0; x < 5; ++x) 
    { 
     mystuff2.values.push_back (MyInt (x)); 
    } 

vector<MyInt>::iterator VITER; 
mystuff2.values.push_back(10); 
mystuff2.values.push_back(7); 

    //error points to the line below 
mystuff2.values.erase(std::remove(mystuff2.values.begin(), mystuff2.values.end(), 2), mystuff2.values.end()); 

    return 0; 

}

오류 메시지

,691,363,210

stl_algo.h : 기능의 '_OutputIterator의 표준 : remove_copy (_InputInputIterator, _InputIterator, const_Tp &) with_InputIterator = __gnu_cxx : normal_iterator __>> OutputIterator = __ __ gnu_cxx :: 정상 반복자>> Tp를 = INT]

연산자 == '에 대한 검색은

Erorr 메시지는 partciular 라인 stl_algo.h의 라인 실질적 라인 1,267, 1,190, 327, 1,263, 208, 212, 216, 220, 228, 232을 위반했다 , 236

+1

"어떤 경기를 운영자 =="무엇 그것에 대해 불분명? myInt와 정수를 비교할 수 있도록'operator =='함수를 정의해야합니다. – jrok

+0

@jrok, 답변이어야합니다. 정말 간단합니다. – chris

답변

2

MyInt 등급에 == 연산자를 오버로드해야합니다. 예를 들어

:

struct MyInt 
{ 

friend ostream &operator<<(ostream &printout, const MyInt &Qn) 
{ 
    printout<< Qn.value << endl; 
    return printout; 
} 

// Overload the operator 
bool operator==(const MyInt& rhs) const 
{ 
    return this->value == rhs.value; 
} 

    int value; 
    MyInt (int value) : value (value) {} 
}; 
+0

해당 연산자는 const 여야합니다. –

+0

@BenjaminLindley 해결되었습니다. 그것을'const'하게 만드는 목적은 무엇입니까? –

+1

그래서 두 개의 const MyInts를 비교할 수있었습니다. –

1

두 가지 문제가 있습니다. 보고있는 오류는 int와 유형 사이의 동등 비교를 정의하지 않았 음을 알려줍니다. 당신의 구조체에, 당신은 하나의 항등 연산자

bool operator==(int other) const 
{ 
    return value == other; 
} 

을 정의해야합니다 물론 다른 방향으로 글로벌 연산자를 정의하지 :

bool operator==(int value1, const MyInt& value2) 
{ 
    return value2 == value1; 
}