
有关c++ vs2013编译器中strcpy_s函数的一个小问题。帮我看看那里出错。
#include<iostream>#include<string>usingnamespacestd;classcstring{intm_size;char*p;pub...
#include<iostream>
#include<string>
using namespace std;
class cstring
{
int m_size;
char *p;
public:
cstring(char * str="");
cstring(const cstring & str);
cstring operator+(const cstring& str);
void operator=(cstring& str);
void print();
};
cstring::cstring(char * str)
{
if (str == NULL)
{
m_size = 0;
p = NULL;
}
else
{
m_size = strlen(str);
p = new char[m_size + 1];
strcpy_s(p, m_size + 1, str);
}
}
cstring::cstring(const cstring& str)
{
if (str.p == NULL)
{
p = NULL;
m_size = 0;
}
else
{
m_size = str.m_size;
p = new char[m_size];
strcpy_s(p,m_size, str.p);
}
}
cstring cstring::operator + (cstring const &str)
{
cstring str1;
str1.m_size = str.m_size + m_size - 1;
str1.p = new char[str1.m_size];
strcpy_s(str1.p,str1.m_size, p);
strcpy_s(&(str.p[m_size]),str1.m_size, str.p); //从str.p的第m_size位开始拷贝。
return str1;
}
void cstring::operator = (cstring &str)
{
str.m_size=m_size;
str.p = new char[str.m_size];
strcpy_s(str.p,str.m_size,p);
}
void cstring::print()
{
cout << p << endl;
cout << m_size << endl;
}
void main()
{
cstring s1("gelin"), s2("tingting"), s3;
s3=s1 + s2;
s3.print();
}
结果本来应该是gelintingting的。但是不知道为什么无法出现结果,后来慢慢试了几次,发现是重载+号那个函数除了问题,但我自己找不出哪里出了问题,请大神帮忙看看。 展开
#include<string>
using namespace std;
class cstring
{
int m_size;
char *p;
public:
cstring(char * str="");
cstring(const cstring & str);
cstring operator+(const cstring& str);
void operator=(cstring& str);
void print();
};
cstring::cstring(char * str)
{
if (str == NULL)
{
m_size = 0;
p = NULL;
}
else
{
m_size = strlen(str);
p = new char[m_size + 1];
strcpy_s(p, m_size + 1, str);
}
}
cstring::cstring(const cstring& str)
{
if (str.p == NULL)
{
p = NULL;
m_size = 0;
}
else
{
m_size = str.m_size;
p = new char[m_size];
strcpy_s(p,m_size, str.p);
}
}
cstring cstring::operator + (cstring const &str)
{
cstring str1;
str1.m_size = str.m_size + m_size - 1;
str1.p = new char[str1.m_size];
strcpy_s(str1.p,str1.m_size, p);
strcpy_s(&(str.p[m_size]),str1.m_size, str.p); //从str.p的第m_size位开始拷贝。
return str1;
}
void cstring::operator = (cstring &str)
{
str.m_size=m_size;
str.p = new char[str.m_size];
strcpy_s(str.p,str.m_size,p);
}
void cstring::print()
{
cout << p << endl;
cout << m_size << endl;
}
void main()
{
cstring s1("gelin"), s2("tingting"), s3;
s3=s1 + s2;
s3.print();
}
结果本来应该是gelintingting的。但是不知道为什么无法出现结果,后来慢慢试了几次,发现是重载+号那个函数除了问题,但我自己找不出哪里出了问题,请大神帮忙看看。 展开
1个回答
展开全部
你定义了这个类,类成员m_size的记录的到底是什么,你没有弄清楚,看构造函数,m_size记录的是字符串的大小,这个大小不包括最后的结束符,但是,你在后续的使用中,包括了结束符,导致strcpy_s函数使用失败
你的错误在于=操作符的重载。
void operator=(cstring str)
{}
隐含的操作数this是左操作数,即用str给this所指向的cstring类对象赋值,而不是给str赋值,你搞混了
修改后的代码:
#include<iostream>
#include<string>
using namespace std;
class mystring
{
int m_size;
char *p;
public:
mystring(char * str="");
mystring(const mystring & str);
mystring operator+(const mystring& str);
void operator=(mystring& str);
void print();
};
mystring::mystring(char * str)
{
if (str == NULL)
{
m_size = 0;
p = NULL;
}
else
{
m_size = strlen(str);
p = new char[m_size + 1];
strcpy_s(p, m_size + 1, str);
}
}
mystring::mystring(const mystring& str)
{
if (str.p == NULL)
{
p = NULL;
m_size = 0;
}
else
{
m_size = str.m_size;
p = new char[m_size + 1];
strcpy_s(p,m_size + 1, str.p);// 赋值的字符数是m_size+1,要包括最后的结束符
}
}
mystring mystring::operator + (mystring const &str)
{
mystring str1;
str1.m_size = str.m_size + m_size;//
str1.p = new char[str1.m_size + 1];//
strcpy_s(str1.p,m_size+1, p);//
strcpy_s(str1.p+m_size,str.m_size+1, str.p); //
return str1;
}
void mystring::operator = (mystring &str)//赋值方向错了
{
m_size = str.m_size;
p = new char[m_size + 1];
strcpy_s(p, m_size + 1, str.p);
}
void mystring::print()
{
cout << p << endl;
cout << m_size << endl;
}
void main()
{
mystring s1("1234"), s2("qwer"), s3;
s3 = s1 + s2;
s3.print();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询