Search code examples
c++sqrtperfect-square

C++ No instance of overloaded function "sqrt" matches the argument list - Trying to find if a struct type array of numbers are perfect squares


I have a homework to do in C++, I'm trying to find if numbers from an array are perfect Square. Also, that array is dinamically allocated. Here is my code:

myVector perfectSquare(myVector *vect)
{
    myVector rez;
    rez.length = 0;

    for (int i = 0; i < vect->length; i++)
        if (vect[i] == sqrt(vect[i])*sqrt(vect[i])) // here it gives error
        {
            addToVector(&rez, vect->arr[i]);
        }

    return rez;
}

Solution

  • vect is a pointer to a struct with an arr field, so you need to determine what field you are looking for:

    sqrt(vect->arr[i]) * sqrt(vect->arr[i])
    

    Please note, that writing vect[i] you mean a lot of vect elements and trying to get the i'th one vect element. But writing vect->arr[i] you mean a pointer to some exact vect element, trying to evaluate it's arr field and get i'th element of arr field.