0
votes

I was going to return the index of an array with string index_of(string value, string data[], int size) but compiling returns the error in the attached code.

string index_of(string value, string data[], int size)
{
    for(int i = 0; i < size; i++)
    {
        if( value[i] = data )
        {
            write(value);
            value.push_back(i);
        }
    }
    value.push_back(-1);
}

error: assigning to 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::value_type' (aka 'char') from incompatible type 'std::__1::string *' (aka 'basic_string<char, char_traits<char>, allocator<char> > *')

if( value[i] = data )

1

1 Answers

2
votes

if( value[i] = data )

This line is an assignment because you are using assignment(=) operator. You should be using comparison operator == for comparison.

value is of type string but data is an array of strings. The statement above tries to assign an array of strings (std::string*) to a character (char) within a string. That is not possible, hence the compiler error.

You should be doing this:

if (value == data[i])

I'm not sure what you are trying to do in this function. Are you trying to return the index within an array where the given string occurs? In that case should you be returning an integer, not a string. You could change your function as follows:

int index_of(string value, string data[], int size)
{
    int index(-1);

    for(int i = 0; i < size; i++)
    {
        if (value == data[i])
        {
            write(value);
            index = i;
            break;
        }
    }

    return index;
}