鱼C论坛

 找回密码
 立即注册
查看: 2133|回复: 1

strcpy_s的用法

[复制链接]
发表于 2017-12-7 10:44:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请问strcpy和strcpy_s该怎么用呢?
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cstring>
  4. using namespace std;

  5. class Dog
  6. {
  7. private:
  8.         char* pName;
  9.         char* pColor;
  10.         static int counter;

  11. public:
  12.         Dog(char* pn, char* pc)
  13.         {
  14.                 pName = new char[strlen(pn) + 1];
  15.                 strcpy(pName, pn);//strcpy和strcpy_s
  16.                 pColor = new char[strlen(pc) +1];
  17.                 strcpy(pColor, pc);//strcpy和strcpy_s
  18.                 counter++;
  19.         }
  20.         int getCounter() {
  21.                 return counter;
  22.         }
  23. };

  24. int Dog::counter = 0;


  25. int main()
  26. {
  27.         Dog d1("小白", "白色");
  28.         Dog d2("小黄", "黄色");
  29.         Dog d3("小红", "红色");
  30.         cout << d1.getCounter() << endl;
  31.         system("pause");
  32.         return 0;
  33. }
复制代码

17,19行换成strcpy和strcpy_s都不行,里面的参数该怎么写呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-12-7 11:06:19 | 显示全部楼层
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cstring>
  4. using namespace std;

  5. class BookShelf
  6. {
  7. private:
  8.         static int MAX_BOOKS;
  9.         int count;
  10.         char* name;
  11.         class  Book
  12.         {
  13.         private:
  14.                 char* title;
  15.                 int price;
  16.         public:
  17.                 Book(char* t, int p)
  18.                 {
  19.                         title = new char[strlen(t) + 1];
  20.                         strcpy(title, t);
  21.                         price = p;
  22.                 }
  23.                 char* getTitle() { return title; }
  24.                 int getPrice() { return price; }
  25.         };
  26.         Book* book[10];
  27. public:
  28.         BookShelf(char* n)
  29.         {
  30.                 name = new char[strlen(n) + 1];
  31.                 strcpy(name, n);
  32.                 count = 0;
  33.         }
  34.         void InsertBook(char* t, int p)
  35.         {
  36.                 if (count==MAX_BOOKS)
  37.                 {
  38.                         cout << "书架已经满了\n" << endl;
  39.                 }
  40.                 book[count++] = new Book(t, p);
  41.         }
  42.         void ListAllBooks()
  43.         {
  44.                 cout << "[" << name << "]" << endl;
  45.                 for (int i = 0; i < count; i++)
  46.                 {
  47.                         cout << book[i]->getTitle() << book[i]->getPrice() << endl;
  48.                 }
  49.         }
  50. };

  51. int BookShelf::MAX_BOOKS = 10;
  52. int main()
  53. {
  54.         BookShelf bks("salkld;sakdl;");
  55.         bks.InsertBook("book1", 23);
  56.         bks.ListAllBooks();

  57.         system("pause");
  58.         return 0;
  59. }
复制代码


这段代码也是一样的错误
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 03:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表