Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.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

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++_String_Substring - Fatal编程技术网

C++ 字符串类=运算符

C++ 字符串类=运算符,c++,string,substring,C++,String,Substring,我正在构建自己的字符串类,但在使用子字符串时遇到了一些问题 // Substring operator // reutns a substring from a given point String String::Substring(int startPosition, int length) const{ if(length==0) length = GetLength()+1; //Takes care of null terminator, im not worr

我正在构建自己的字符串类,但在使用子字符串时遇到了一些问题

// Substring operator
// reutns a substring from a given point
String String::Substring(int startPosition, int length) const{
    if(length==0)
        length = GetLength()+1; //Takes care of null terminator, im not worried about if length is imputed yet
    char* result = new char[length-startPosition]; // Assume it's not negative for the sake of just getting it to work, It would only be negative if it's user error
    for(int i=startPosition; i<length; i++)
        result[i] = Text[i]; //Since it will always  go from a given point to the end, the null terminator will transfer in the for loop.

    return result;
}

//将C-string分配给此字符串
字符串和字符串::运算符=(常量字符*文本){
//首先删除现有字符串
删除[]文本;
//+1表示空终止符
int trueLength=GetLength(text)+1;
//分配新内存
Text=新字符[trueLength];
//将源中的所有字符复制到文本中
for(int i=0;i

我不知道我做错了什么,谢谢你的帮助。

当你分配
长度起始位置时,你很可能使用了负数

仅使用长度进行新操作:

char* result = new char[length];
编辑:

开始从
i=0
复制,并将上次复制的字符后的字节设置为空:

for(int i=0; i<length; i++)
    result[i] = Text[i+startPosition];

result[i] = '\0';

for(int i=0;i考虑使用
char*
构造函数创建字符串对象时会发生什么:

String::String(const char* text){
  *this = text;
}
所有成员都尚未初始化,您可以调用
操作符=

String& String::operator = (const char* text){
  // Delete the existing string first
  delete[] Text;
您可以删除成员
文本
,即使尚未对其进行初始化。删除单元化指针会产生未定义的行为。在这种情况下,该行为是一个例外


在调用
运算符=
之前,在构造函数中将
文本初始化为null,或者在构造函数中完成所有工作,而不是赋值运算符。

最大的问题是您正在编写自己的字符串类。为什么?有更多有建设性的方法来正确学习该语言。@Chad这就是他正在做的-i这不关你的事。为什么。如果你帮不上忙,不要费心回答。我不认为做“长度”是个好主意。如果我想要一个长度为1GB的5个字符的子字符串怎么办?我应该将内存占用加倍吗?我宁愿检查偏移量边界,然后执行他所做的操作。@VladLazarenko,length不是想要的字符数吗?他也在写入结果并添加startPosition,这可能会超出缓冲区(肯定会写错位置).是的,当我初始化长度时,空终止符会得到处理,但我没有这样做,因此在给定预定义长度时,它会实际添加+1。我已经考虑过这一点,但谢谢you@wzsun另外,你应该用size\u t来表示这个,而不是int。我确实考虑过这个问题,但是代码实际上是一成不变的,因为我的教授告诉m他约束了字符串的哪一部分,你可以自由选择哪一部分?如果他说“总是从构造函数调用operator=”,然后这样做,但不要调用未定义的行为。上面的行为是受约束的,所以我不认为我应该处理它。老实说,当我认为将调用返回时,它将只输出结果,甚至不调用operator=经过一番思考后,我决定更改它,因为这是使它工作的唯一方法。
String::String(const char* text){
  *this = text;
}
String& String::operator = (const char* text){
  // Delete the existing string first
  delete[] Text;