これはベクトルの基本的なバイナリ検索関数です。オブジェクトのget関数にアクセスしたいがエラーが発生する。- >演算子を介してベクトルオブジェクトの関数にアクセスすることはできません
bool binFindInVec(vector<Client> *vec,string sur){
int from,to,pos;
from = 0;
to = vec->size()-1;
while(from<=to){
pos = (from+to)/2;
if(vec[pos]->getSurname() == sur){
return true;
}
else if(vec[pos]->getSurname() > sur){
to = pos-1;
}
else{
from = pos + 1;
}
}
return NULL;
}
エラー:
In function 'bool binFindInVec(std::vector*, std::string)':
176 14 [Error] base operand of '->' has non-pointer type 'std::vector'
179 19 [Error] base operand of '->' has non-pointer type 'std::vector'
を参照解除する必要があります。 '(* vec)[pos] .getSurname()'が必要な場合があります。 – songyuanyao
また、参照を渡す代わりに、 'bool binFindInVec(vector&vec、string sur){'を使うと、複雑さを軽減できます。それでは、単に 'vec [pos] .getSurname()'を使うことができます。 –
参照ではなくベクトルへのポインタを渡す必要がある理由について考えることもできます。 –