【问题标题】:No instance of overloaded function "strcpy_s" matches the argument list没有重载函数“strcpy_s”的实例与参数列表匹配
【发布时间】:2016-04-20 22:28:04
【问题描述】:

由于某种原因,字符不能进入 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  

【问题讨论】:

  • 只是一个错字:bookNamechar 而不是 char *
  • 嗯@fukanchik?我了解了指针但是??这是怎么回事?
  • 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....我还是个初学者,所以我不知道...虽然它完全有道理..因为您需要传递指针来实际更改值!非常感谢!!

标签: c++ windows strcpy


【解决方案1】:

正确。 strcpy 及其家人使用char*,而不是char。他们在 C 字符串上工作。而且您通常不能将bookName 放在单个字符中。

也就是说,欢迎来到 21 世纪。我们现在使用std::string,容易得多。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-16
  • 2013-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多