鱼C论坛

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

strcpy_s的用法

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

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

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

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

class Dog
{
private:
        char* pName;
        char* pColor;
        static int counter;

public:
        Dog(char* pn, char* pc)
        {
                pName = new char[strlen(pn) + 1];
                strcpy(pName, pn);//strcpy和strcpy_s
                pColor = new char[strlen(pc) +1];
                strcpy(pColor, pc);//strcpy和strcpy_s
                counter++;
        }
        int getCounter() {
                return counter;
        }
};

int Dog::counter = 0;


int main()
{
        Dog d1("小白", "白色");
        Dog d2("小黄", "黄色");
        Dog d3("小红", "红色");
        cout << d1.getCounter() << endl;
        system("pause");
        return 0;
}
17,19行换成strcpy和strcpy_s都不行,里面的参数该怎么写呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

class BookShelf
{
private:
        static int MAX_BOOKS;
        int count;
        char* name;
        class  Book
        {
        private:
                char* title;
                int price;
        public:
                Book(char* t, int p)
                {
                        title = new char[strlen(t) + 1];
                        strcpy(title, t);
                        price = p;
                }
                char* getTitle() { return title; }
                int getPrice() { return price; }
        };
        Book* book[10];
public:
        BookShelf(char* n)
        {
                name = new char[strlen(n) + 1];
                strcpy(name, n);
                count = 0;
        }
        void InsertBook(char* t, int p)
        {
                if (count==MAX_BOOKS)
                {
                        cout << "书架已经满了\n" << endl;
                }
                book[count++] = new Book(t, p);
        }
        void ListAllBooks()
        {
                cout << "[" << name << "]" << endl;
                for (int i = 0; i < count; i++)
                {
                        cout << book[i]->getTitle() << book[i]->getPrice() << endl;
                }
        }
};

int BookShelf::MAX_BOOKS = 10;
int main()
{
        BookShelf bks("salkld;sakdl;");
        bks.InsertBook("book1", 23);
        bks.ListAllBooks();

        system("pause");
        return 0;
}

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-1 03:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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