Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 自定义字符串类在c++;_C++_String - Fatal编程技术网

C++ 自定义字符串类在c++;

C++ 自定义字符串类在c++;,c++,string,C++,String,我的自定义字符串运算符+=有一个问题,因此如果我想向自定义字符串中添加一个const char*,那么它不会添加它,它只是字符串。我可以让它与其他自定义字符串对象一起工作,但不能与c字符串一起工作 String.cpp中的代码 String::String(){ length =0; capacity = 1024; myStr = new char[0]; } String::String (const String& s) { length = s.g

我的自定义字符串运算符+=有一个问题,因此如果我想向自定义字符串中添加一个const char*,那么它不会添加它,它只是字符串。我可以让它与其他自定义字符串对象一起工作,但不能与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];
}

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

str[length+1]=s[i]-不应该是
长度+i
?你想将
s
中的字符写入
str
中的连续位置…抱歉@TonyDelroy让我来解决这个问题,我正在测试内容,但删除了它,忘记了重新添加,我现在编辑帖子。很好,将其归结为相关代码<你好,世界
fout
文件
-这些都不相关。调试器是您的朋友。您可以轻松地跟踪这些函数并查看发生了什么。如果它更像是一个函数,我会自己运行它。您需要决定是否希望字符串以null结尾,如果是,则“长度”是否计算null结尾符。现在,两者兼而有之,不能很好地工作。
int main() {

    
    

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


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

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