错误 C2664:show_info:无法将参数 2 从 'char [20]' 转换为"字符"
error C2664: show_info: cannot convert parameter 2 from 'char [20]' to 'char
我有一个小结构:
struct price
{
char name[20];
char shop[20];
int pr;
price *next;
};
一个不起作用的功能:
void show_info(price *&head, char cur)
{
bool found = 0;
price *temp = new price;
temp->name = cur;
for (price *i=head; i!=NULL; i=i->next)
if (temp == i)
{
cout<< i->shop << i->pr;
found = 1;
}
if (!found)
cout << "The the good with such name is not found";
delete temp;
}
主文件:
int main()
{
price *price_list=NULL;
char inf[20];
list_fill(price_list);
cout << "Info about goods: ";
show_list(price_list); //there is no problem
cout <<"Input goods name you want to know about: ";
cin >> inf;
cout << "The info about good " << inf << show_info(price_list,inf)<<endl;
system("pause");
return 0;
}
我需要修复我的功能,这样它才能正常工作。
如前所述,错误为c2664。
void show_list(price *&head, char cur)
应该是
void show_list(price *&head, char cur[] )
当您通过inf
时,即show_info(price_list,inf)
处的char [20]
PS:可能还有其他问题
按照以下方式重写函数
#include <cstring>
//...
void show_info( const price *head, const char *cur )
{
bool found = false;
const price *i = head;
for ( ; i != NULL && !found; i = i->next )
{
found = strcmp( i->name, cur ) == 0;
}
if ( found )
{
cout<< i->shop << i->pr;
}
else
{
cout << "The the good with such name is not found";
}
}
相关文章:
- 将无符号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转换为字符串构造字符串