C++ 错误:无法将参数1从std::string转换为char*。困惑于为什么它会给我这个错误。
因此,我再次需要一些帮助,这个课程。也许是因为我累了,但我似乎找不到我肯定在这里犯下的逻辑错误。这是我的密码:C++ 错误:无法将参数1从std::string转换为char*。困惑于为什么它会给我这个错误。,c++,C++,因此,我再次需要一些帮助,这个课程。也许是因为我累了,但我似乎找不到我肯定在这里犯下的逻辑错误。这是我的密码: #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;
#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> ";
}
#包括“Book.h”
使用名称空间std;
无效添加(字符*,字符*,整数);
无效删除(int&);
作废清单();
整数计数;
书架[4];
int main()
{
串入;
字符串N;
字符串A;
int-Y;
int-Num;
做
{
cout>N>>A>>Y;
加(N,A,Y);
}
else if(In.compare(“remove”)==0)
{
cout>Num;
删除(Num);
}
else if(In.compare(“list”)==0)
{
列表();
}
}而(cin!=“退出”);
返回0;
}
void add(字符串N、字符串A、整数Y)
{
if(Bookshelf[4].IsEmpty()==false)
难道你忘了修改文件顶部的原型了吗
它仍然说
void add (char*, char*, int);
应该是
void add (string, string, int);
你的远期申报有误
void add (char*, char*, int);
一定是-
void add (string, string, int);
此外,如果数组大小为N
,则可访问的索引为0
到N-1
Book Bookshelf [4];
// .....
if (Bookshelf[4].IsEmpty() == false) // There is no object at Bookshelf[4]
// Accessible indexes are 0 to 3
您需要使声明与定义匹配。在声明中使用char*
在定义中使用string
如果您想使用C字符串,请参阅。如果您想使用字符串,请记住“&”以获取引用-节省制作副本的时间。无论哪种方式,都可以使原型和函数签名匹配。啊,谢谢。我不敢相信我错过了这一点。显示了等待到最后一分钟才能完成的功能。