2016-06-15 2 views
-5

C++ 3을 더 잘 준비하기 위해 마지막 학기 C++ 클래스의 모든 과제를 코딩하기로 결정했지만 String 클래스를 통과하는 방법을 이해하지 못합니다. 또는 두 개의 문자열을 연결하고 결과를 주 cpp 파일에 표시하기 위해 필요한 단계가 필요합니다. 나의하여 Main.cpp에서 :문자열 및 문자열 :: Concat (const char str [])

#include <iostream> 
using namespace std; 

#include "String.h" 

int main() 
{ 
    String Str1; 
    String Str2("this is a test"); 
    String Str3(Str2); 
    String Str4("bruh"); 
    int result; 

    cout << "Testing Display: " << endl; 
    Str2.Display(); 
    cout << endl; 
    cout << "Testing displayLine: " << endl; 
    Str2.displayLine(); 
    cout << endl; 

    result = Str2.Compare(Str3); 
    if (result < 0) 
    { 
     Str2.Display(); 
     cout << " comes before " << endl; 
     Str3.Display(); 
     cout << endl; 
    } 

    else 
     if (result > 0) 
     { 
      Str3.Display(); 
      cout << " comes before " << endl; 
      Str2.Display(); 
     } 
     else 
     { 
      Str3.Display(); 
      cout << " is equal to " << endl; 
      Str2.Display(); 
     } 
    cout << endl; 

    result = Str2.Compare("wxyz"); 
    Str1.Copy(Str3); 

    cout << "Str1 contains " << Str1.length() <<" characters"<< endl; 


    cout << "Concatenation: "; 
    Str2.Concat(Str4); 
     cout << endl; 

    return 0; 
} 

내 String.cpp에서 :

#include <iostream> 
using namespace std; 
#include <string.h> 
#include "String.h" 

#pragma warning(disable:4996) 

String::String() 
{ 
    NumChars = 0; 
    MaxSlots = 0; 
    pChar = new char[NumChars+1]; 
    pChar[0] = '\0'; 
} 

String::String(const char Str[]) 
{ 
    NumChars = strlen(Str); 
    pChar = new char[NumChars + 1]; 
    strcpy(pChar, Str); 
} 

String::String(const String & Str) 
{ 
    NumChars = Str.NumChars; 
    pChar = new char[NumChars + 1]; 
    strcpy(pChar, Str.pChar); 
} 

String::~String() 
{ 
    delete[] pChar; 
} 

int String::Compare(const String & Str) const 
{ 
    return strcmp(pChar, Str.pChar);  //case sensitive 
} 

int String::Compare(const char Str[]) const 
{ 
    return strcmp(pChar, Str);   //case sensitive 
} 

String& String::Copy(const String & Str) 
{ 
    if (this != &Str) 
    { 
     if (MaxSlots < Str.NumChars) 
     { 
      delete[]pChar; 
      MaxSlots = Str.NumChars; 
      pChar = new char[NumChars + 1]; 
     } 
     else; 
     NumChars = Str.NumChars; 
     strcpy(pChar, Str.pChar); 
    } 
    else; 
    return *this; 
} 

String& String::Copy(const char Str[]) 
{ 
    delete[] pChar; 
    NumChars = strlen(Str); 
    MaxSlots = NumChars; 
    pChar = new char[MaxSlots + 1]; 
    return *this; 
} 

String& String::Concat(const String & Str) 
{ 
    pTemp = new char[NumChars+1]; 
    strcpy(pTemp, pChar); 
    strcat(pTemp, Str.pChar); 
    delete[]pChar; 
    pChar = pTemp; 
    return *this; 
} 

String & String::Concat(const char Str[]) 
{ 
    return *this; 
    /* 
    NumChars = strlen(Str); 
    MaxSlots = NumChars; 


    delete[] pChar; 
    MaxSlots = MaxSlots + NumChars; 
    NumChars = NumChars + strlen(Str); 
    pChar = new char[MaxSlots + 1]; */ 
} 

void String::Display() const 
{ 
    cout << pChar; 
} 

void String::displayLine() const 
{ 
    cout << pChar; 
} 

내 String.h에서 :

#ifndef STRING_H 
#define STRING_H 

class String 
    { 
    public: 
        String(); //default constructor 
        String(const char[]); 
        String(const String &); //copy constructor 
        ~String(); 
     int   Compare(const String &) const; 
     int   Compare(const char[])const; 
     String&  Copy(const String&); 
     String&  Copy(const char[]); 
     String&  Concat(const String&); 
     String&  Concat(const char[]); 
     void  Display()const; 
     void  displayLine() const; 
     int   length() const; 

    private: 
     char * pChar; 
     char *pTemp; 
     int  NumChars; 
     int  MaxSlots; 
}; 

inline int String::length() const 
{ 
    return NumChars; 
}; 

#endif 
+3

왜 사람들은 여전히 ​​자신의 삶 건물을 낭비하고 문자열 클래스? 대신 클럽 활동을하십시오. – Bathsheba

+0

'String & String :: Concat (const String & Str)'은 연결된 길이가 길이가 0 인 문자열이 아니면 범위를 벗어나는 액세스를 유발합니다. – MikeCAT

+0

질문이 있으십니까? – juanchopanza

답변

0

MaxSlots의 사용을 잘 모르겠다. 할당 된 크기는 pChar (마이너스 1)입니까? 그렇다면 코드에서 설정/업데이트/사용을 잊어 버린 부분이 있습니다.

"String 클래스를 통과하는 방법"과 관련하여 "두 문자열을 연결하는 데 필요한 단계가 무엇인지"와 관련해서는 무엇을 의미하는지 이해하지 못합니다. 객체에 이미있는 문자의 수.

우선, 전자 Reserve() 메서드를 만드는 것이 좋습니다. 같은 [주의 : 코드는 테스트하지]

String& String::Reserve (int n) 
{ 
    if (n > MaxSlots) 
    { 
     MaxSlots = n; 
     pTemp = new char[MaxSlots+1]; 
     strcpy(pTemp, pChar); 
     delete[]pChar; 
     pChar = pTemp; 
    } 
} 

다음,이 방법으로

String& String::Concat(const String & Str) 
{ 
    NumChars += Str.NumChars; 
    Reserve(NumChars); 
    strcat(pChar, Str.pChar); 
    return *this; 
} 

String & String::Concat(const char * Str) 
{ 
    if (Str) 
    { 
     NumChars += strlen(Str); 
     Reserve(NumChars); 
     strcat(pChar, Str); 
    } 
    return *this; 
} 

PS 당신의 Concat() 방법을 다시 내 나쁜 영어 죄송는

+0

흠, 내가 그것을 실행할 때 작동하지 않는다. 내 코드가했던 것과 똑같은 일을한다. 메인에서 concat에 도달했을 때 멈췄다. 나는 여전히 혼란 스럽다. ( – Jonathon

+0

@Jonathon - 당신은'Display()'를 했나? 결과? – max66

1

당신은 연결된 문자열의 길이가 될 것으로 기대 2 개의 캐릭터 라인의 길이의 합 따라서 :

String& String::Concat(const String & Str) 
{ 
    pTemp = new char[NumChars + Str.NumChars + 1]; 
    strcpy(pTemp, pChar); 
    strcat(pTemp, Str.pChar); 
    delete[]pChar; 
    pChar = pTemp; 
    return *this; 
} 

당신은하지 strcat와에 의해 더이를 최적화 할 수 있습니다() - 보내고 있지만, strcpy를() - 이미 문자열 길이를 알고, (AN은 pTemp 두 번째로 추가 된 offset)를 두 번 보내고.