push_back 结构化为向量
push_back struct into vector
//prototype
void Split(char c, vector <MyString> &outputVector) const
//partial code inside split function
// create new MyString object to push into output vector
MyString substr;
substr.mString = newString;
substr.mLength = size;
// push new item
outputVector.push_back(substr);
在我越过 outputVector.push_back()
行后,mString
数据成员未保留。
//I have two constructors
MyString()
{
mString = NULL;
mLength = 0;
}
/*************************************************
* MyList copy constructor
* creates a deep copy of a MyString item
************************************************/
MyString(const MyString ©)
{
mString = new char[copy.mLength];
int i;
for(; i < copy.mLength; i++)
{ mString[i] = copy.mString[i]; }
mString[i] = '[=12=]';
mLength = copy.mLength;
}
http://www.cplusplus.com/reference/vector/vector/push_back/
push_back 将值复制到矢量。 MyString class 是否有正确定义的复制构造函数,复制 mString 成员?我想这可能是你的问题。
复制构造函数的正确版本
MyString(const MyString ©)
{
mString = new char[copy.mLength + 1];
int i = 0;
for(; i < copy.mLength; i++)
{ mString[i] = copy.mString[i]; }
mString[i] = '[=10=]';
mLength = copy.mLength;
}
您正在使用一个未初始化的变量,它是 undefined behavior
int i;
for(; i < copy.mLength; i++)
这里我们不知道 i
是什么所以任何事情都可以进行,但很可能 i
大于 copy.mLength
所以我们永远不会进入 for 循环。为了获得正确的行为,将 i
设置为 0,如
int i = 0;
您还有另一个问题
mString[i] = '[=12=]';
当我们到达那条线时 i == copy.mLength
但数组只有 copy.mLength
的大小,所以我们已经结束了,因为数组是基于 0 索引的。您很可能需要将分配更改为
mString = new char[copy.mLength + 1];
给你 space 作为空终止符。
我认为有2个错误,你已经完成了
1.for(i; i < copy.mLength; i++)
{ mString[i] = copy.mString[i]; }
你必须提到,循环将从哪里开始。
2.mString = 新字符[copy.mLength + 1];
mString[i] = '\0';
我想,你得到了答案:)
//prototype
void Split(char c, vector <MyString> &outputVector) const
//partial code inside split function
// create new MyString object to push into output vector
MyString substr;
substr.mString = newString;
substr.mLength = size;
// push new item
outputVector.push_back(substr);
在我越过 outputVector.push_back()
行后,mString
数据成员未保留。
//I have two constructors
MyString()
{
mString = NULL;
mLength = 0;
}
/*************************************************
* MyList copy constructor
* creates a deep copy of a MyString item
************************************************/
MyString(const MyString ©)
{
mString = new char[copy.mLength];
int i;
for(; i < copy.mLength; i++)
{ mString[i] = copy.mString[i]; }
mString[i] = '[=12=]';
mLength = copy.mLength;
}
http://www.cplusplus.com/reference/vector/vector/push_back/
push_back 将值复制到矢量。 MyString class 是否有正确定义的复制构造函数,复制 mString 成员?我想这可能是你的问题。
复制构造函数的正确版本
MyString(const MyString ©)
{
mString = new char[copy.mLength + 1];
int i = 0;
for(; i < copy.mLength; i++)
{ mString[i] = copy.mString[i]; }
mString[i] = '[=10=]';
mLength = copy.mLength;
}
您正在使用一个未初始化的变量,它是 undefined behavior
int i;
for(; i < copy.mLength; i++)
这里我们不知道 i
是什么所以任何事情都可以进行,但很可能 i
大于 copy.mLength
所以我们永远不会进入 for 循环。为了获得正确的行为,将 i
设置为 0,如
int i = 0;
您还有另一个问题
mString[i] = '[=12=]';
当我们到达那条线时 i == copy.mLength
但数组只有 copy.mLength
的大小,所以我们已经结束了,因为数组是基于 0 索引的。您很可能需要将分配更改为
mString = new char[copy.mLength + 1];
给你 space 作为空终止符。
我认为有2个错误,你已经完成了 1.for(i; i < copy.mLength; i++) { mString[i] = copy.mString[i]; } 你必须提到,循环将从哪里开始。 2.mString = 新字符[copy.mLength + 1]; mString[i] = '\0'; 我想,你得到了答案:)