2014-10-26 58 views
-4

我有一个小的结构:错误C2664:show_info:无法从 '的char [20]' 转换参数2至“字符

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。

+2

它是'show_info()'还是'show_list()'? – NPE 2014-10-26 20:16:30

+0

erm ...是它的show_info()。我的错。 – Grafit 2014-10-26 20:19:45

+0

可能值得用'std :: string'替换'char [20]'。我看到你正在试图进行某种搜索;通过使用'std :: string'你可以避免麻烦。 – anatolyg 2014-10-26 20:33:44

回答

0

重写功能通过以下方式

#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"; 
    } 
} 
+0

我们使'const price * head'和'const char * cur'使'strcmp'工作? – Grafit 2014-10-26 20:52:00

+0

@Grafit该函数在没有这些限定符的情况下工作。它们只是意味着函数内部的列表和字符串都不会被改变。你也可以使用字符串作为第二个参数。 – 2014-10-26 20:54:12

+0

这个功能看起来更好。但该项目仍然无法正常工作。 @vlad – Grafit 2014-10-26 21:02:08

1
void show_list(price *&head, char cur) 

应该

void show_list(price *&head, char cur[]) 

为你传递infchar [20]show_info(price_list,inf)

PS:有可能是其他问题太

+0

true。现在有2个错误。错误c2440:=:无法从'char []'转换为'char [20]' – Grafit 2014-10-26 20:27:15

+1

@Grafit试着分析这些错误,对于这个简单的代码他们不会太难,如果你是_really_迷失回来在SO,新帖子 – P0W 2014-10-26 20:28:38

+0

呃。无论如何。 – Grafit 2014-10-26 20:29:46

相关问题