Jump to content
seaza

un mic ajutor?

Recommended Posts

Posted

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

Posted

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.

Posted

#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;
}

Posted

El folosea versiunea microsoft a strcpy si anume strcpy_s, probabil eroarea primita este datorita faptului ca a inclus "string" si nu versiunea c si anume "string.h"

Pentru referinet aici

Posted (edited)

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

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...