-5
我正在创建自己的字符串类,但在使用strlen()来读取字符串的字符时遇到了问题。我的str类错误
/****str.h****/
class str
{
private:
char *m_ptr;
unsigned int m_size;
//unsigned int m_capacity;
public:
str();
str (const char *);
str (const str &);
~str(){if (m_size != 0) delete [] m_ptr;};
char *data() {return m_ptr;};
//Sobrecarga de operadors.
str operator=(str);
friend std::ostream& operator<<(std::ostream &, str);
};
我得到了错误,使用与C字符串常量初始化的构造函数。
/****str.cpp****/
//Default constructor.
str :: str()
{
m_ptr = new char [1];
m_ptr[0] = '\0';
m_size = 0;
//m_capacity = 10;
}
str :: str(const char *sm_ptr)
{
m_size = strlen(sm_ptr); //HERE IS WHERE THE ERROR OCCURS.
m_ptr = new char[m_size + 1];
strcpy(m_ptr, sm_ptr); //Copies the C string pointed by source into the array pointed by destination, including the terminating null character
}
//Copy constructor.
str :: str(const str &right)
{
m_ptr = new char [right.m_size];
strcpy (m_ptr, right.m_ptr);
m_size = right.m_size;
}
str str::operator=(str right)
{
if (m_size != 0) delete [] m_ptr;
m_ptr = new char [right.m_size + 1];
strcpy(m_ptr, right.m_ptr);
m_size = right.m_size;
return *this;
}
std::ostream &operator<<(std::ostream &strm, str obj)
{
strm << obj.m_ptr;
return strm;
}
0x0053fdd0 {m_ptr = 0xcccccccc m_size = 3435973836} STR *
欢迎来到Stack Overflow。请阅读[Stack Overflow:How to ask](http://stackoverflow.com/questions/how-to-ask)和[Jon Skeet的问题清单](http://msmvps.com/blogs/jon_skeet/archive/2012) /11/24/stack-overflow-question-checklist.aspx)来找出如何提出一个很好的问题,这将产生很好的答案。 –
_'I使用了''错误,特别是哪个错误?请编辑你的问题。 –