alex.b
alex.b

Reputation: 21

error: invalid conversion from 'char' to 'char*'

I have a small problem. I keep getting this error: error: invalid conversion from 'char' to 'char*' for this part of the code (list<lst>* listSearch(list<lst> *LST, char* wrd, KEY key)).

What am I doing wrong here?

enum KEY {code, disease} key;

struct lst
{
    char *_code;
    char *_disease;
    lst* next;
};


bool isPartOf(char* word, char* sentence)
{
    unsigned int i=0;
    unsigned int j=0;

    for(i;i < strlen(sentence); i++)
    {
        if(sentence[i] == word[j])
        {
            j++;
        }
    }

    if(strlen(word) == j)
        return true;
    else
        return false;
}

list<lst>* listSearch(list<lst> *LST,char* wrd,KEY key)
{
    list<lst> resultList;
    list<lst>* result;

    switch(key)
    {
    case code:
        for(list<lst>::iterator i = LST->begin(); i != LST->end(); i++)
        {
            if(isPartOf(wrd, *i._code))
            {
                resultList.push_back(*i);
            }
        }
        break;

    case disease:
        for(list<lst>::iterator i = LST->begin(); i != LST->end(); i++)
        {
            if(isPartOf(wrd, *i->_disease))
                resultList.push_back(*i);
        }
        break;
    }

    result = &resultList;

    return result;
}

Upvotes: 2

Views: 628

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311146

It looks like a compiler bug. At least the compiler shall issue another error message.

The problem is related to operator priorities. This statement

if(isPartOf(wrd, *i._code))

must be written like

if(isPartOf(wrd, ( *i )._code))

It seems that the compiler issued the error for this statement

if(isPartOf(wrd, *i->_disease))

It must be written like

if(isPartOf(wrd, i->_disease))

because expression *i->_disease has type char but you have to pass to the function an object of type char *

And using this function can result in undefined behaviour because it returns pointer to a local object.

list<lst>* listSearch(list<lst> *LST,char* wrd,KEY key)
{
    list<lst> resultList;
    list<lst>* result;

    //...

    result = &resultList;

    return result;
}

Take into account that function isPartOf is logically wrong. For example when word is equal "ab" and sentence is equal to "a1111b" the function will return true. I do not think you mean this logic.

Upvotes: 2

Related Questions