0

I am writing a callback function which has a reference to a int vector passed to it in a structure. When I try to access an element in the vector using subscript operator[], Intellisense indicates that the == cannot compare the two elements specifically the error is error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion). But when using at() function there is no problem.

//body of call back function
searchInfo* argVal = (searchInfo*) Parameter;
for(int i = argVal->inclStartPos; i < argVal->exclEndPos; ++i){
    if(argVal->numVector[i] == argVal->searchNum)//problem here
        argVal->result = true;
//this is the structure passed through pointer

struct searchInfo{
int inclStartPos;
int exclEndPos;
vector<int>* numVector; 
int searchNum;
bool result;
};

Since the [] operator and at() function of a vector work almost(the difference doesn't matter here) in the same manner, why the error?

1 Answer 1

6

Actually, you are comparing vector < int > with int, since the field numVector has type

vector<int>*

Simply speaking, you've declared an array of vector's. Operator [] will return a value of type vector.

Probably you have misspelled the declaration. Maybe you wanted to declare numVector as follows:

vector<int> numVector;
Sign up to request clarification or add additional context in comments.

3 Comments

Not sure why this was downvoted. It may not be expressed as clearly as possible, but it's right - numVector is a pointer-to-vector, which is compatible with array-of-vector. Hence numVector[i] is a vector<int>; but numVector->at(i) would work.
@Chowlett: Could you please elaborate on why would numVector->at(i) would work. AFAIK the '==' operator has same data types on both sides , so the expression should work but it doesn't.
@AquaAsh - numVector is a pointer, not an actual vector. Hence numVector[i] will be interpreted as subscripting into an array vector<int>[] from the address numVector, not as taking an element from a vector<int>. numVector[i] == searchNum compares vector<int> with int.numVector->at(i) is calling at on the object pointed to by numVector - it's equivalent to *numVector.at(i)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.