개체를 비교하기 위해 우선 순위 큐에서 사용할 비교 함수가 필요합니다. 객체는 객체의 비율에 따라 정렬되어야합니다. 어떤 이유로 RatioCompare 함수가 작동하지 않거나 컴파일되지 않습니다.개체 비교 함수 우선 순위 큐 C++
In member function
bool RatioCompare::operator()(const Item&, const Item&) const
:joulethief.cpp:58
: error: passing ‘const Item’ as ‘this’ argument ofdouble Item::getRatio()
discards qualifiers joulethief.cpp:59: error: passing ‘const Item’ asthis
argument ofdouble Item::getRatio()
discards qualifiers
너희들이 좀 걸릴 수 있습니다 :
컴파일러는 다음과 같은 오류를 준다? 여기
struct RatioCompare
{
bool operator()(const Item &i1, const Item &i2) const
{
double i1value= i1.getRatio();
double i2value= i2.getRatio();
return i1value < i2value;
}
};
내가 대기열 및 벡터 라이브러리 모두 프로그램으로 포함했다 나는 그것을 선언하고 다음 주 내에서 작동하는지 확인하는 테스트 코드 ...
priority_queue<Item, vector<Item>, RatioCompare > pq;
for(int i=0; i<n; i++)
{
pq.push(tosteal[i]);
}
while(!pq.empty())
{
Item consider= pq.top();
cout<< "Name: "<< consider.getName()<< "Ratio "<< consider.getRatio()<<endl;
pq.pop();
}
입니다 잘.
당신이보고있는 어떤 오류 :
그래서, 단지
Item::getRatio
의 정의를 변경? –멤버 함수 'bool RatioCompare :: operator() (const 항목 & const 항목 &) const': joulethief.cpp : 58 : 오류 : 'double 항목 :: getRatio()'의 'this'인수로 'const 항목 전달' ''double item :: getRatio() '의'this '인수가'한정어 '를 버리는 것으로 전달한다. –
'Item :: getRatio()'를'const'로 표시 할 필요가있다. 자격이된다. – ArchbishopOfBanterbury