i'm trying to implement string class in C++,
#ifndef STRING
# define STRING
class String{
private:
char *Buffer = new char;
inline unsigned long Length(const char *);
inline unsigned long Length(char *);
inline unsigned long Length(String);
inline unsigned long Length(String *);
public:
~String(void){
delete Buffer;
}
unsigned long Size = 0;
void Equal(const char *);
void Equal(char *);
void Equal(String *);
void Equal(String);
char *Get(void);
//Something is wrong in Add method.
char *Add(const char *);
char *Add(char *);
char *Add(String *);
char *Add(String);
//Something is wrong in Add method
char *Multiply(unsigned long *);
char *Multiply(unsigned long);
char& operator[](const unsigned long Index);
const char& operator[](const unsigned long Index) const;
};
# include "StringIO.hpp"
#endif
StringIO.hpp:
#ifndef STRINGIO
# define STRINGIO
inline unsigned long String::Length(String S){return S.Size;}
inline unsigned long String::Length(String *S){return S->Size;}
void String::Equal(String S){this->Equal(S.Buffer);}
void String::Equal(String *S){this->Equal(S->Buffer);}
inline unsigned long String::Length(const char *S){
unsigned long Count = 0; while(S[Count] != '\0'){ Count++;} return Count;
}
inline unsigned long String::Length(char *S){
unsigned long Count = 0; while(S[Count] != '\0'){ Count++;} return Count;
}
void String::Equal(const char *S){
unsigned long Count = 0; while(Count <= this->Length(S)){
this->Buffer[Count] = S[Count];
Count++;
} this->Size = Count - 1;
}
void String::Equal(char *S){
unsigned long Count = 0; while(Count <= this->Length(S)){
this->Buffer[Count] = S[Count];
Count++;
} this->Size = Count - 1;
}
char *String::Get(void){ return this->Buffer;}
/*Something is wrong in here*/
char *String::Add(String *S){return Add(S->Buffer);}
char *String::Add(String S){return Add(S.Buffer);}
char *String::Add(const char *S){
char *Copy = this->Get(); for(unsigned long Count = 0; Count <= Length(S); Count++){
Copy[this->Size + Count] = S[Count];
} return Copy;
}
char *String::Add(char *S){
char *Copy = this->Get(); for(unsigned long Count = 0; Count <= Length(S); Count++){
Copy[this->Size + Count] = S[Count];
} return Copy;
}
/*Something is wrong in here*/
#endif
I write a method for adding Strings, and a simple program adding Strings with this method and prints it to screen:
int main(void){
String *X = new String;
String *Y = new String;
X->Equal("Hello world!\n");
Y->Equal("Hello world!\n");
std::cout << X->Add(Y);
delete X; delete Y; //The line giving crash
return 0;
there is not compiling error but when i run it, it gives core dumped error. How i can fix it? And be merciful, i'm a beginner in C++.
Aucun commentaire:
Enregistrer un commentaire