2016-04-20 47 views
0

匹配出於某種原因,一個字符不能在strcopy_s去(); ...沒有實例參數列表

#include <iostream> 
#include <cstring> 

using namespace std; 

struct DATE { 
    int year; 
    int month; 
    int date; 
}; 
struct Book { 
    char name[50]; 
    char author[50]; 
    int id; 
    DATE date; 
}; 

int main() { 
    Book book1; 
    DATE date1; 
    char bookName, bookAuthor; 
    int date, year, month; 

    cout << "Date Of Publishing? " << endl; 
    cin >> date; 
    cout << "Month Of Publishing?" << endl; 
    cin >> month; 
    cout << "Year Of Publishing?" << endl; 
    cin >> year; 
    date1.year = year; 
    date1.month = month; 
    date1.date = date; 

    cout << "Book Name ? " << endl; 
    cin >> bookName; 
    cout << "Book Author" << endl; 
    cin >> bookAuthor; 
    strcpy_s(book1.name, bookName); 
    strcpy_s(book1.author, bookAuthor); 
    return 0; 
} 

給我的錯誤:

Severity Code Description Project File Line Suppression State 
Error (active)  no instance of overloaded function "strcpy_s" matches the argument list Struct c:\Users\Amanuel\Documents\Visual Studio 2015\Projects\Struct\Struct\Source.cpp 38 

Severity Code Description Project File Line Suppression State 
Error (active)  no instance of overloaded function "strcpy_s" matches the argument list Struct c:\Users\Amanuel\Documents\Visual Studio 2015\Projects\Struct\Struct\Source.cpp 39 

Severity Code Description Project File Line Suppression State 
Error C2665 'strcpy_s': none of the 2 overloads could convert all the argument types Struct c:\users\amanuel\documents\visual studio 2015\projects\struct\struct\source.cpp 38 



Severity Code Description Project File Line Suppression State 
Error C2665 'strcpy_s': none of the 2 overloads could convert all the argument types Struct c:\users\amanuel\documents\visual studio 2015\projects\struct\struct\source.cpp 39 
+0

只是一個錯字:'bookName'是'char'而不是'char *' – fukanchik

+0

呵呵@fukanchik?我瞭解指針,但??這裏的問題如何? – amanuel2

+2

https://msdn.microsoft.com/en-us/library/td1esda9.aspx'strcpy_s(char *,size_t,const char *);'最後一個是'const char *'在調用時:'char bookName,bookAuthor; ... strcpy_s(book1.name,bookName);' – fukanchik

回答

3

正確。 strcpy及其家族取char*,而不是char。他們使用C字符串。無論如何,你通常無法將bookName放入單個字符中。

這就是說,歡迎來到21世紀。我們現在使用std::string,要容易得多。

+0

我喜歡他如何接受答案,但[在他的下一篇文章中完全忽略它](http://stackoverflow.com/questions/36756910/unexpected-output-of-c-code/) –

相關問題