strcpy_s的用法
请问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;
strcpy(pName, pn);//strcpy和strcpy_s
pColor = new char;
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都不行,里面的参数该怎么写呢? #include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
class BookShelf
{
private:
static int MAX_BOOKS;
int count;
char* name;
classBook
{
private:
char* title;
int price;
public:
Book(char* t, int p)
{
title = new char;
strcpy(title, t);
price = p;
}
char* getTitle() { return title; }
int getPrice() { return price; }
};
Book* book;
public:
BookShelf(char* n)
{
name = new char;
strcpy(name, n);
count = 0;
}
void InsertBook(char* t, int p)
{
if (count==MAX_BOOKS)
{
cout << "书架已经满了\n" << endl;
}
book = new Book(t, p);
}
void ListAllBooks()
{
cout << "[" << name << "]" << endl;
for (int i = 0; i < count; i++)
{
cout << book->getTitle() << book->getPrice() << endl;
}
}
};
int BookShelf::MAX_BOOKS = 10;
int main()
{
BookShelf bks("salkld;sakdl;");
bks.InsertBook("book1", 23);
bks.ListAllBooks();
system("pause");
return 0;
}
这段代码也是一样的错误
页:
[1]