错误:从'char'到'char*'的转换无效
error: invalid conversion from 'char' to 'char*'
我有一个小问题。我不断收到此错误:错误:这部分代码从"char">到"char*"的转换无效(list<lst>* listSearch(list<lst> *LST, char* wrd, KEY key)
(。
我在这里做错了什么?
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;
}
它看起来像一个编译器错误。至少编译器应发出另一条错误消息。
该问题与操作员优先级有关。此声明
if(isPartOf(wrd, *i._code))
必须写成
if(isPartOf(wrd, ( *i )._code))
似乎编译器为此语句发出了错误
if(isPartOf(wrd, *i->_disease))
它必须写成
if(isPartOf(wrd, i->_disease))
因为表达式 *i->_disease
具有 char 类型,但您必须将类型为 char *
的对象传递给函数
使用此函数可能会导致未定义的行为,因为它返回指向本地对象的指针。
list<lst>* listSearch(list<lst> *LST,char* wrd,KEY key)
{
list<lst> resultList;
list<lst>* result;
//...
result = &resultList;
return result;
}
考虑到函数isPartOf
在逻辑上是错误的。例如,当word
等于"ab"并且sentence
等于"a1111b"时,函数将返回true。我不认为你的意思是这个逻辑。
相关文章:
- 将无符号char*转换为std::istream*C++
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 将函数参数"const char*"转换为"std::string_view"是
- C ++如何在使用"tolower"时将char转换为int
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- JavaCPP 错误:无法将参数"1"的"char*"转换为"Abc*
- 为什么从 char 转换为 std::byte 可能是未定义的行为?
- 如何在虚幻引擎4中将char*转换为TCHAR?
- C++:将模板类型 char 转换为 std::string
- strcmp/char* 转换无法按预期工作
- C++中的SQLite char*转换
- 无法从 const char* 转换为 const char *&
- 在 C++ 中将 char 转换为 int,而无需符号位传播
- 如果必须包含西里尔字符,如何将 char* 转换为 std::string?
- 试图使用strCMP时的char转换错误
- 无法从'const char *'转换为'LPCTSTR'
- 将 std::vector<char> 转换为字符* 会导致字符缺陷
- 使用 static_cast 将 char 转换为 int c++
- 如何在 C++(MFC) 中将 char* 转换为 LPCTSTR
- 无法将const char转换为字符串构造字符串