自定义C类型字符串类的问题

发布于 2024-12-01 14:32:49 字数 6476 浏览 1 评论 0原文

我有作业需要使用 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);
} 

I have homework that I need to make a custom string class using C type strings. Most of it seems okay but I am getting a runtime error as soon as I test it and it isn't performing all my tests like it should. Specifically my += operator seems to have something wrong with it but I can't understand what.

I can't add in or change any of the prototypes and I should use C++ constructs over C type where possible.

Thanks!

#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);
}

This is what I'm testing with:

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;
}

EDIT: I should have added that I need to get the tests to work as part of the homework.

EDIT 2: Here are the functions I changed so far.

//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;
}

EDIT 3: Unfortunately changing it to below did nothing really. It might be the = operator that's causing the problem as it now fails just after test 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);
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云淡风轻 2024-12-08 14:32:49

您有几个涉及空终止符和您的多个函数的问题(其中一些问题您已在编辑中修复,但无论如何我都会涵盖所有这些问题,因此这是一个彻底的答案)。

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

You have several problems involving the null-terminator with several of your functions(some of these you have fixed in your edits, but I'm just going to cover them all anyway, so that this is a thorough answer).

  1. In the constructor converting from const char *, you are not copying over the null-terminator to strPtr. (fixed)
  2. In your copy constructor, you are not allocating enough room for the null-terminator. (fixed)
  3. In your assignment operator, you are not allocating enough room for the null-terminator. (fixed ... not exactly, see #5)
  4. In your assignment operator, you are not copying over the null-terminator.
  5. You added a new problem in your assignment operator when you fixed the allocation. You are now setting strSize to the wrong size.
故事还在继续 2024-12-08 14:32:49

有几个地方需要 +1 来保留字符串的结尾 \0。例如,在复制构造函数

strPtr = new char[str.strSize];

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

但是您应该仔细检查所有这些类型的错误,并单独测试每个函数(也许使用一个好的测试套件,您知道,在类本身之前编写):)

There are several places in which you need +1 to reserve the ending \0 for strings. For example, in the copy constructor

strPtr = new char[str.strSize];

(that should be str.strSize+1) and in operator=(). These may be the sources of your problems.

BUT you should carefully review all these kind of errors, and test each function separately (maybe with a good test suite, you know, written before the class itself) :)

浅暮の光 2024-12-08 14:32:49

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

Your char* converting constructor doesn't NUL-terminate the content (it reserves space for a NUL, but doesn't store one), which causes strcpy and strcat to fail later.

满栀 2024-12-08 14:32:49

由于还没有人指出这一点,因此您的 strSize 在复制构造函数中仍然未定义;并在您的赋值运算符中。

如果字符串将与 C 样式字符串(即 char*、strcopy 等)交互,则只需将字符串以 null 终止即可。

Since no one has pointed it out yet, your strSize remains undefined in your copy constructor; and in your assignment operator.

You only need to null terminate your strings if they will be interfacing with C style strings (ie char*, strcopy etc).

_畞蕅 2024-12-08 14:32:49

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

operator +=() should return a reference -- TStr&. You are returning a copy, which is probably not what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文