cCCwhite
cCCwhite
2019-05-07 17:18

C++程序,关于strcpy_s使用报错(新手初学,虚心请教)

  • c++

strcpy_s或是strcpy都尝试过,
前者报错 error C2065: 'strcpy_s' : undeclared identifier
后者报错 error C2660: 'strcpy' : function does not take 3 parameters
全程序

#include<iostream>
#include<cstring>
using namespace std;
class Name
{
    public:
        Name(char *pn);
        Name(const Name &Obj);
        ~Name();
        void setName(char*);
        void showName();
    protected:
        char *pName;
        int size;
};
Name::Name(char *pn)
{
    cout<<"Constructibng"<<pn<<endl;
    pName=new char[strlen(pn)+1];
    if(pName!=0)    strcpy_s(pName,strlen(pn)+1,pn);
    size=strlen(pn);
}
Name::Name(const Name& Obj)
{
    cout<<"Copying"<<Obj.pName<<"into its own block\n";
    pName=new char[strlen(Obj.pName)+1];
    if(pName!=0)    strcpy_s(pName,strlen(Obj.pName)+1,Obj.pName);
    size=Obj.size;
}
Name::~Name()
{
    cout<<"Destructing"<<pName<<endl;
    pName[0]='\0';
    delete  []pName;
    pName=NULL;
    size=0;
}
void Name::setName(char *pn)
{
    delete  []pName;
    pName=new char[strlen(pn)+1];
    if(pName!=0)    strcpy_s(pName,strlen(pn)+1,pn);
    size=strlen(pn);
}
void Name::showName()
{
    cout<<pName<<endl;
}
int main()
{
    Name Obj1("NoName");
    Name Obj2=Obj1;
    Obj1.showName();
    Obj2.showName();
    Obj1.setName("sudongpo");
    Obj2.setName("dufu");
    Obj1.showName();
    Obj2.showName();
}

使用软件为 Visual C++ 6.0

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答