首页 > 解决方案 > 自定义字符串类在 C++ 中添加 char*

问题描述

我的自定义字符串运算符 += 有问题,所以如果我想将 const char* 添加到我的自定义字符串 String,那么它不会添加它,它只是 String。我可以让它与其他自定义 String 对象一起使用,但不能与 c 字符串一起使用。

String.cpp 中的代码

String::String(){
    length =0;
    capacity = 1024;
    myStr = new char[0];
}
String::String (const String& s)
{
    length = s.getLength();
    myStr   = new char[length];
    for (unsigned j=0; j < length; j++){
        myStr[j] = s[j];
    }

}
String::String(char c){
    length = 1;
    myStr = new char(c);
}

String::String(const char* val){
    if(val!= nullptr){
        int counter = 0;
        while(val[counter]!='\0')counter++; //Find length of the char array
        length= counter;
        myStr = new char[counter];
        for(int i =0;i<counter;i++){
            myStr[i]=val[i];
        }


    }else{
        length = 1;
        myStr = new char[length];
        myStr[0] = '\0';
    }
}
ostream& operator<< (std::ostream& os, const String& s)
{
    os<<s.myStr;

    return os;
}
istream& operator>> (std::istream& is, String& s)
{
    char* c = new char[1000];
    is >> c;
    s = String(c);
    delete[] c;

    return is;
}
String::~String(){
    delete[] myStr;
}
int String::getIndex(char getInd) const {
    for(int i = 0;i<length;i++){
        if(myStr[i]==getInd){
            return i;
        }
    }
}
unsigned String::getLength() const{
    return length;

}
String& String::operator+= (const String& s){
    unsigned len = length+s.getLength();
    char* str=new char[len];
    for(int i = 0;i<length;i++){
        str[i]=myStr[i];

    }
    for(int i = 0;i<s.length;i++){
        str[length+1]=s[i];
    }
    delete myStr;
    length = len;
    myStr= str;
    return *this;

}
String& String::operator+=(const char *c) {
    unsigned l =0;
    while(c[l] !='\0'){
        l++;
    }
    unsigned len =length+l;//new length of new string
    char* str = new char[len];
    for(int i = 0;i<getLength();i++) {
        str[i] = myStr[i];
    }
    for(int j = 0; j<l;j++){
        str[len+j]=c[j];
    }
    delete myStr;
    length = len;
    myStr = str;
    return *this;
}
String& String::operator=(const String& s){
    if(this == &s){
        return *this;
    }
    delete myStr;
    length = s.getLength();
    myStr = new char[length];
    for(int i = 0;i<length;i++){
        myStr[i]=s[i];
    }

}
char& String::operator[](unsigned i){
    if(i>=length) throw 1;
    return myStr[i];
}
char String::operator[] (unsigned j) const
{
    if (j >= length) throw 1;
    return myStr[j];
}

main.cpp 中的代码

int main() {

    
    

    char c[] = {'h','i'}


    
    String add = "iscool";
    String str="strworks";
    str+=add;
    str+=c;

    cout<<str<<endl;
    return 0;
}

该程序现在在控制台中的结果是:

    
    strworks

我不确定对 const char* c += 运算符函数有什么改变,我相信这就是错误所在。我也尝试在 getline 函数中使用此字符串,但我尝试 getline(file,String& s, ','); 有一个错误,我将不胜感激有关如何使用我的新 String 类实现 getline 函数的任何提示。String str 应该是输出第二行中的 histrworks,但它是空白的。我已经重新编写了 += 运算符的逻辑,但它似乎不起作用。让我知道是否有任何我需要提供的更多信息可以帮助你们找到问题。该程序的最终目标是读取电影评论文件并训练算法来确定评论是正面还是负面。

标签: c++string

解决方案


推荐阅读