2012-02-11 578 views
0

所以我再次需要这个课程的一些帮助。也许它是我累了,但我似乎无法找到我确定在这里做出的逻辑错误。这里是我的代码:错误:无法将参数1从std :: string转换为char *。困惑为什么它给了我这个错误。

#include "Book.h" 


using namespace std; 

void add (char*, char*, int); 
void remove (int&); 
void list(); 

int Count; 

Book Bookshelf [4]; 

int main() 
{ 
    string In; 
    string N; 
    string A; 
    int Y; 
    int Num; 

    do 
    { 
     cout << "Bookshelf> "; 
     getline(cin, In); 

     if (In.compare("add") == 0) 
     { 
      cout << "Bookshelf> Enter book: "; 
      cin >> N >> A >> Y; 
      add (N,A,Y); 
     } 

     else if (In.compare ("remove") == 0) 
     { 
      cout << "Bookshelf> Select number: "; 
      cin >> Num; 
      remove (Num); 
     } 

     else if (In.compare("list") == 0) 
     { 
      list(); 
     } 

    } while (cin != "quit"); 

    return 0; 
} 

void add (string N, string A, int Y) 
{ 
    if (Bookshelf[4].IsEmpty() == false) 
     cout << "Error!" << endl; 
    else 
    { 
     Bookshelf[Count] = Book (N,A,Y); 
     Count++; 
    } 
    cout << "Bookshelf> "; 
} 

在该行add(N,A,Y);出现的错误,但对我的生活我不知道为什么它说。他们都看起来像std ::字符串给我。任何人都可以向我解释这个吗?

回答

3

您忘记了在文件顶部修改原型。

还在说

void add (char*, char*, int); 

应该

void add (string, string, int); 
+0

啊谢谢。我不能相信我错过了这一点。显示等到最后一分钟完成的情况。 – triple07 2012-02-11 05:08:22

2

你有一个错误的向前声明。

void add (char*, char*, int); 

必须是 -

void add (string, string, int); 

而且,如果数组大小为N,可访问索引0N-1

Book Bookshelf [4]; 

// ..... 

if (Bookshelf[4].IsEmpty() == false) // There is no object at Bookshelf[4] 
             // Accessible indexes are 0 to 3 
2

您需要使声明与定义匹配。在声明中,您使用char *定义您使用string

如果要使用C字符串,请参阅c_str。如果你想使用字符串,请记住'&'作为参考 - 保存复制。无论哪种方式使原型和功能签名匹配。

相关问题