我正在开发一个使用 C 字符串的类,我创建了一个返回调用对象(C 字符串)长度的成员函数。当我运行代码时,我得到 Project5.exe 中的 0x0F63F6E0 (ucrtbased.dll) 抛出异常:0xC0000005:访问冲突读取位置 0x00000000。 我不知道如何解决这个问题。我不太确定我需要多少代码,但希望下面的代码片段就足够了。
MyString::MyString(const char* aString) //memberString is a c-string object
{
memberString = new char[length() + 1];
strcpy(memberString, aString);
}
int MyString::length() //Exception gets raised here
{
return strlen(memberString); //Exception gets raised here
}
最佳答案
你的问题是
length()
需要memberString
返回存储数据的大小,memberString
需要length()
来创建。
我认为你的构造函数不应该依赖于其他成员函数。
关于:
MyString::MyString(const char* aString) //memberString is a c-string object
{
memberString = new char[strlen(aString) + 1];
strcpy(memberString, aString);
}
关于c++ - 不明白为什么我会收到异常 : Access violation reading location 0x00000000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55764293/