Bonjour,
j'essaye de créer une class String, mais je n'arrive pas à faire fonctionner mon opérateur d'affectation.
Je vous transmet mon code :
le . h :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #ifndef _Sring #define _Strng class String { private: char* _string; public: String(); //constructeur par défaut ~String(); // destructeur String(const String &stringACopier); String operator=(const String &aString); }; #endif
et le .cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 #include "String.h" #include <iostream> using namespace std; String::String(){ _string = new char(); } String::~String(){ delete [] _string; } String::String(const String &stringACopier){ if( this != &stringACopier){ _string = new char[strlen(stringACopier._string)+1]; if (_string != NULL){ strcpy (_string, stringACopier._string); } } } String String::operator=(const String &aString){ if( this != &aString){ if (_string != NULL){ delete [] _string; } _string = new char[strlen(aString._string)+1]; if (_string != NULL){ strcpy (_string, aString._string); } } return (*this); }
j'éspère que vous pourrez m'aider car je ne vois vraiment pas d'où vient le problème.
Car quand dans le main je fais :
merci d'avance et joyeuses fêtes.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 String s1; s1 ="azd"; //j'ai une erreur ici String s2 = s1;//mais pas là
Partager