Jump to content
seaza

un mic ajutor?

Recommended Posts

am urmatorul progr:


#include<iostream>
#include<string>
using namespace std;
class persoana
{
int varsta;
public:
char *pnume;
persoana(char *n,int v):varsta(v)
{
pnume=new char[strlen("Anonim")+1];
strcpy_s(pnume, n);
}
int spune_varsta()
{
return varsta;
}
};
int main()
{
persoana p1("vasilache",45);
persoana p2=p1;//apel constructor de copiere
cout<<"\n"<<p1.pnume<<"are"<<p1.spune_varsta()<<"ani";
cout<<"\n"<<p2.pnume<<"are"<<p2.spune_varsta()<<"ani";
strcpy(p2.pnume,"Gigi");
cout<<"\n"<<p1.pnume<<"are"<<p1.spune_varsta()<<"ani";
cout<<"\n"<<p2.pnume<<"are"<<p2.spune_varsta()<<"ani";
system("PAUSE");
return 0;
}

am o eroare la linia 12.. ma ajuta cnva?:-S

Link to comment
Share on other sites

Linia numarul 12 este

strcpy_s(pnume, n);

Spune ce anume eroare iti da.

Daca am gasit eu referinta corecta pentru strcpy_s ai nevoie ca al 2-lea argument sa fie numarul de caractere ce trebuie copiate.

Adica

strcpy_s(pnume, (sizeof(n)/sizeof(n[0])), n)

prin (sizeof(n)/sizeof(n[0]) poti afla numarul de elemente dintr-un char array.

Totusi pentru corectitudine te rog sa postezi eroarea.

Link to comment
Share on other sites


#include<iostream>
#include<string>
[COLOR=#40e0d0][B]#include <cstring>
#include <cstdlib>[/B][/COLOR]
using namespace std;
class persoana
{
int varsta;
public:
char *pnume;
persoana(char *n,int v):varsta(v)
{
pnume=new char[strlen("Anonim")+1];
[COLOR=#40e0d0][B]strcpy(pnume, n);[/B][/COLOR]
}
int spune_varsta()
{
return varsta;
}
};
int main()
{
persoana p1("vasilache",45);
persoana p2=p1;//apel constructor de copiere
cout<<"\n"<<p1.pnume<<"are"<<p1.spune_varsta()<<"ani";
cout<<"\n"<<p2.pnume<<"are"<<p2.spune_varsta()<<"ani";
strcpy(p2.pnume,"Gigi");
cout<<"\n"<<p1.pnume<<"are"<<p1.spune_varsta()<<"ani";
cout<<"\n"<<p2.pnume<<"are"<<p2.spune_varsta()<<"ani" << endl;
system("PAUSE");
return 0;
}

Link to comment
Share on other sites

lucrez in visual studio, iar strcpy nu merge daca nu incluzi si header.ele d care zice @cmiN, si anume: #include <cstring>

#include <cstdlib>

...de care eu uitasem:D

iar in visual studio nu mai ai <__.h>, d asta folosisem strcpy_s..

greetings @cmiN!;)

Edited by seaza
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...