2015-12-04 45 views
2

我必須使用string.h(當您看到代碼時您會明白)構建一個String類。請原諒我可憐的英語。 我不知道問題出在哪裏。它只是崩潰沒有任何錯誤消息。使用string.h創建簡單的String類

String.h

#ifndef _STRING_H 
#define _STRING_H 

class String{ 
public: 
    char* s1; 
    char* s2; 
    char* stemp; 


    //The Constructors 
    String(); 
    String(char* so); 
    String(char* so, char* st); 

    //Member Functions 
    int slength(char* s1); //Calculate the length of Sting 
    void scopy(char* so,const char* st); // copy string value to another String 


}; 



#endif // _STRING_H 

String.cpp

#include <string.h> 
#include "String.h" 

//The Constructors 
String::String(){} 

String::String(char* so){ 
    s1 = so; 
} 
String::String(char* so, char* st){ 
    s1 = so; 
    s2 = st; 
} 


//Member Functions 

int String::slength(char* so){ 
    return strlen(so); 
} 

void String::scopy(char* so,const char* st){ 

    strcpy(so,st); 
} 

main.cpp

#include <iostream> 
#include "String.h" 


using namespace std; 

int main() 
{ 

    String str("Hello","World"); 


    cout<<"The First String is : "<<str.s1<<endl; 
    cout<<"The Second String is : "<<str.s2<<endl; 
    cout<<"-------------------------------------"<<endl; 

    cout<<"The First String contains "<<str.slength(str.s1)<<" Letters"<<endl; 
    cout<<"The Second String contains "<<str.slength(str.s2)<<" Letters"<<endl; 

    cout<<"-------------------------------------"<<endl; 

    cout<<"Copying The Second String in the First String . . ."<<endl; 
    str.scopy(str.s1,str.s2); 
    cout<<"The First String is : "<<str.s1<<endl; 
    cout<<"The Second String is : "<<str.s2<<endl; 


    return 0; 
} 
+1

你有沒有嘗試調試你的代碼?更新:今天是什麼,人們試圖寫入字符串文字? C++中的字符串文字是(被認爲是)只讀的。 –

+0

是:編程接收到的信號SIGSEGV,分段故障。 在strcat()(C:\ Windows \ system32 \ msvcrt.dll)中 我沒有使用strcar(); ?!? – Mexyzer

+0

如果您使用GDB,您是否嘗試使用'where'命令? – MikeCAT

回答

0

我只是解決了這個問題

(STRING.H)

#ifndef STRING_H 
#define STRING_H 

class String{ 
    public: 
     //Data Members 
     char* s1; 
     int len ; 

     //The Constructors 
     String(); 
     String(char* so); 

     //Member Functions 
     int slength(String &s1); //Calculate the length of Sting 
     void scopy(String &so,String &st); // copy string value to another String 
     int scompare(String &so, String &st); // compare two string and returns which one is bigger 
     void scombine(String &so, String &st); // combine two strings into one string 
     //The Destructor 
     ~String(); 

}; 



#endif // STRING_H 

(String.cpp)

#include <string.h> 
#include "String.h" 

//The Constructors 
String::String(){ 
    len = 0; 
    s1 = new char[len + 1]; 
} 

String::String(char* so){ 
    len = strlen(so); 
    s1 = new char[len + 1]; 
    strcpy(s1, so); 

} 


//Member Functions 

int String::slength(String &so){ 
    return strlen(so.s1); 
} 

void String::scopy(String &so,String &st){ 

    strcpy(so.s1,st.s1); 
} 

int String::scompare(String &so, String &st){ 
    return strcmp(so.s1,st.s1); 

} 


void String::scombine(String &so, String &st){ 
    len = so.len + st.len; 
    delete s1; 
    s1 = new char[len + 1]; 
    strcpy(s1,so.s1); 
    strcat(s1,st.s1); 

} 


//The Destructor 
String::~String(){} 

(main.cpp中)

#include <iostream> 
#include "String.h" 


using namespace std; 

int main() 
{ 

String str1("Hello"); 
String str2("World"); 
String str; 


cout<<endl<<"The First String is : "<<str1.s1<<endl; 
cout<<"The Second String is : "<<str2.s1<<endl; 
cout<<endl<<"-------------------------------------"<<endl; 

cout<<endl<<"The First String contains "<<str.slength(str1)<<" Letters"<<endl; 
cout<<"The Second String contains "<<str.slength(str2)<<" Letters"<<endl; 

cout<<endl<<"-------------------------------------"<<endl; 

cout<<endl<<"After Comparing the Two Strings we Find out That"<<endl; 
switch(str.scompare(str1,str2)){ 
    case 1: 
     cout<<"The First String is Bigger than the Second String"; 
     break; 
    case 0: 
     cout<<"The two Strings are the equal"; 
     break; 
    default: 
     cout<<"The Second String is Bigger than the First String"; 
} 

cout<<endl<<endl<<"-------------------------------------"<<endl; 

cout<<endl<<"After Combining The two Strings"<<endl; 
str.scombine(str1,str2); 
cout<<str.s1<<endl; 


cout<<endl<<"-------------------------------------"<<endl; 

cout<<endl<<"After copying The Second String in the First String . . ."<<endl; 
str.scopy(str1,str2); 
cout<<"The First String is : "<<str1.s1<<endl; 
cout<<"The Second String is : "<<str2.s1<<endl; 


    return 0; 
} 
3

字符串文字分配給str.s1,並作爲第一個參數傳遞0的。這是非常糟糕的,因爲修改字符串文字是不允許的。

+0

我該怎麼辦? – Mexyzer

+1

@Mexyzer你的字符串類不應該複製指針,而是分配內存並使用'strcpy'來保存自己的字符串副本。 – MicroVirus

+0

對不起,我不明白你在說什麼。也許是因爲我的英語。 – Mexyzer