Custom C类型字符串类的问题

时间:2011-08-28 22:11:25

标签: c++ cstring

我有功课需要使用C类型字符串制作自定义字符串类。大多数似乎都没问题,但是一旦我测试它就会出现运行时错误,并且它没有按照它应该执行我所有的测试。特别是我的+ =运算符似乎有问题,但我无法理解。

我无法添加或更改任何原型,我应尽可能使用C类型的C ++构造。

谢谢!

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

//Default constructor to initialize the string to null
TStr::TStr() {
    strPtr = 0;
    strSize = 0;
}  
//constructor; conversion from the char string
TStr::TStr(const char *str) {
    int i=0;
    while (str[i] != '\0') {
        ++i;
    }
    strSize = i;
    strPtr = new char [i+1];
    for (i=0; i < strSize; ++i) {
        strPtr[i] = str[i];
    }
} 
//Copy constructor
TStr::TStr(const TStr& str) {
    strPtr = new char[str.strSize];
    strcpy(strPtr, str.strPtr);
}
//Destructor
TStr::~TStr() {
    delete[] strPtr;
}

//subscript operators-checks for range
char& TStr::operator [] (int i) {
    assert (i >= 0 && i < strSize);
    return strPtr[i];
}
const char& TStr::operator [] (int i) const {
    assert (i >= 0 && i < strSize);
    return strPtr[i];
}

//overload the concatenation oprerator
TStr TStr::operator += (const TStr& str) {
    char *buffer = new char[strSize + str.strSize + 1];
    strcpy(buffer, strPtr);
    strcat(buffer, str.strPtr);
    delete [] strPtr;
    strPtr = buffer;
    return *this;
}

//overload the assignment operator
const TStr& TStr::operator = (const TStr& str) {
    if (this != &str) {
        delete[] strPtr;
        strPtr = new char[strSize = str.strSize];
        assert(strPtr);
        for (int i=0; i<strSize; ++i) {
            strPtr[i] = str.strPtr[i];
        }
    }
    return *this;
}

//overload two relational operators as member functions
bool TStr::operator == (const TStr& str) const {
    int value = strcmp(strPtr, str.strPtr);
    if (value == 0) {
        return true;
    } else {
        return false;
    }
    /*int counter=0;
    for (int i=0; i < strSize; ++i) {
        if (strPtr[i] == str.strPtr[i]) {
            ++counter;
        }
    }
    if (counter == strSize) {
        return true;
    } else {
        return false;
    }
    return (strPtr == str.strPtr && strSize == str.strSize);*/
}
bool TStr::operator < (const TStr& str) const {
    return (strPtr < str.strPtr && strSize < str.strSize);
}
//the length of the string
int TStr::size() {
    return strSize;
}

//Overload the stream insertion and extraction operators.
ostream& operator << (ostream& out, const TStr& str) {
int size = str.strSize;
for (int i=0; i < size; ++i) {
    out << str[i];
}
return out;
}
istream& operator >> (istream& in, TStr& str) {
return in;
}

//overload two other relational operators as global functions
bool operator != (const TStr& S1, const TStr& S2) {
    return !(S1 == S2);
}
bool operator <= (const TStr& S1, const TStr& S2) {
    return (S1 < S2 || S1 == S2);
}
bool operator > (const TStr& S1, const TStr& S2) {
    return !(S1 < S2);
}
bool operator >= (const TStr& S1, const TStr& S2) {
    return !(S1 < S2 || S1 == S2);
}

//overload the concatenation operator as a global function
TStr operator + (const TStr& str1, const TStr& str2) {
    //return (str1 += str2);
    //return (str1 + str2);
}

这就是我正在测试的内容:

int main() {
authors();
TStr str1 = "VENI";         //initialize str1 using 
                                //the assignment   operator 
const TStr str2("VEDI");    //initialize str2 using the
                                    //conversion constructor
TStr str3; //initialize str3 to null
TStr str4; //initialize str4 to null

cout << "\nTest 1: str1: " << str1 << " str2 " << str2 
     << " str3 " << str3 << " ###.\n" ;          //Test 1

if (str1 <= str2)                               //Test 2  
    cout << "\nTest 2: " << str1 << " is less "
         << "than " << str2 << endl;            
else                                            
    cout << "\nTest 2: " << str2 << " is less "
         << "than " << str1 << endl;            

str1=" Pride is what we have.";

str3 = str1;                                    //Test 3
cout << "\nTest 3: The new value of str3 = "
     << str3 << endl;                          


str3 += " Vanity is what others have. ";              
cout<<"\nTest 4: The str3: '" << str3<<"'  \nhas ";        //test 4
cout<< countVowels(str3)<<" vowels "<< endl;

/*TStr str5 = str1 + str2 + str3;
cout<<"\nTest 5: The str5: '" << str5<<"' \nhas ";        //test 5
cout<<countVowels(str5)<<" vowels \n";

cout<<"\nTest 6: The str3 again: " << str3 <<endl;    //test 6

cout<<"\n\n Bye, Bye!";*/


return 0;
}

编辑:我应该补充一点,我需要让测试作为家庭作业的一部分。

编辑2:这是我到目前为止所改变的功能。

//constructor; conversion from the char string
TStr::TStr(const char *str) {
    int i=0;
    while (str[i] != '\0') {
        ++i;
    }
    strSize = i;
    strPtr = new char [i+1];
    for (i=0; i < strSize; ++i) {
        strPtr[i] = str[i];
        strPtr[i + 1] = '\0'; //<- this
    }
} 
//Copy constructor
TStr::TStr(const TStr& str) {
    strPtr = new char[str.strSize + 1]; //<-this
    strcpy(strPtr, str.strPtr);
}
//overload the concatenation oprerator
TStr TStr::operator += (const TStr& str) {
    char *buffer = new char[strSize + str.strSize + 1]; //<- this
    strcpy(buffer, strPtr);
    strcat(buffer, str.strPtr);
    delete [] strPtr;
    strPtr = buffer;
    return *this;
}

//overload the assignment operator
const TStr& TStr::operator = (const TStr& str) {
    if (this != &str) {
        delete[] strPtr;
        strPtr = new char[strSize = str.strSize + 1]; //<- this
        assert(strPtr);
        for (int i=0; i<strSize; ++i) {
            strPtr[i] = str.strPtr[i];
        }
    }
    return *this;
}

编辑3:不幸的是将其改为以下并没有真正做到。它可能是导致问题的=运算符,因为它现在在测试3之后就失败了。

//constructor; conversion from the char string
TStr::TStr(const char *str) {
    int i=0;
    while (str[i] != '\0') {
        ++i;
    }
    strSize = i;
    strPtr = new char [i+1];
    strcpy(strPtr, str);
} 

5 个答案:

答案 0 :(得分:1)

你有几个问题涉及null终结器和你的几个函数(其中一些你已经在你的编辑中修复了,但我只是将它们全部覆盖,所以这是一个彻底的答案)。< / p>

  1. 在从const char *转换的构造函数中,您没有将null终止符复制到strPtr。(已修复)
  2. 在您的复制构造函数中,您没有为空终止符分配足够的空间。(已修复)
  3. 在赋值运算符中,您没有为空终止符分配足够的空间。(已修复...不完全,请参阅#5)
  4. 在赋值运算符中,您不是要复制空终止符。
  5. 修复分配时,您在赋值运算符中添加了一个新问题。您现在将strSize设置为错误的大小。

答案 1 :(得分:1)

在某些地方,您需要+1为字符串保留结尾\0。例如,在复制构造函数

strPtr = new char[str.strSize];

(应该是str.strSize+1)和operator=()。这些可能是您的问题的根源。

但是你应该仔细检查所有这些错误,并分别测试每个功能(可能有一个好的测试套件,你知道,在类本身之前编写) :)

答案 2 :(得分:1)

您的char*转换构造函数没有NUL终止内容(它为NUL保留空间,但不存储空间),这会导致strcpystrcat失败后面。

答案 3 :(得分:1)

由于还没有人指出它,你的strSize在你的拷贝构造函数中仍未定义;并在你的任务操作员。

如果你的字符串与C样式字符串接口(即char *,strcopy等),你只需要null来终止字符串。

答案 4 :(得分:0)

operator +=()应该返回引用 - TStr&。您将返回一份副本,这可能不是您想要的。