gpt4 book ai didi

c++ - "Unhandled exception thrown: read access violation. _First was nullptr"错误是什么意思?

转载 作者:行者123 更新时间:2023-11-30 05:25:23 26 4
gpt4 key购买 nike

我编译了我的代码,但它抛出“异常抛出:读取访问冲突。_第一个是 nullptr。”

我真的不知道这意味着什么,因为我仍然是 C++ 的初学者。我真的需要你的帮助来解决这个问题,因为我已经为这段代码苦苦挣扎了好几天,这太令人沮丧了......

提前致谢。

#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;

class MyString {
public:

//default constructor
MyString();

MyString(char* chars);

//copy constructor
MyString(const MyString &);

int length() const;

//destructor
~MyString();

//operator overloads
char& operator[](int index);
friend MyString operator+(const MyString& newWord, const MyString& newWord2);
MyString& operator+=(const MyString& newWord);
friend ostream& operator<<(ostream& newWord, const MyString& newWord2);
friend istream& operator >> (istream& newWord, MyString& newWord2);
friend bool operator==(const MyString& newWord, const MyString& newWord2);
friend bool operator!=(const MyString& newWord, const MyString& newWord2);
friend bool operator<(const MyString& newWord, const MyString& newWord2);
friend bool operator<=(const MyString& newWord, const MyString& newWord2);
friend bool operator>(const MyString& newWord, const MyString& newWord2);
friend bool operator>=(const MyString& newWord, const MyString& newWord2);

private:
char* value;
int size;
};

//default constructor
MyString::MyString() {

value = NULL;
size = 0;
}

//copy constructor
MyString::MyString(const MyString& newWord) {

//perform a deep copy to copy each of the value to a new memory
size = newWord.size;
char* newCopy = new char[size];

for (int ii = 0; ii < size; ii++) {
newCopy[ii] = newWord.value[ii];
}
}

//constructor with an argument
MyString::MyString(char* chars) {

//give the value and the size
value = chars;
size = strlen(chars);
}

//find length
int MyString::length() const {

return size;
}

//find the value of each index
char& MyString::operator[](int index) {

return value[index];
}

//operator + (concatenate)
MyString operator+(const MyString& newWord, const MyString& newWord2) {

MyString concatenated;
concatenated = strcat(newWord.value, newWord.value);
return concatenated;

}

//operator += (append)
MyString& MyString::operator+=(const MyString& newWord) {

char * newMemory = value;
value = new char[strlen(value) + newWord.length() + 1];
strcpy(value, newMemory);
strcat(value, newWord.value);
if (size != 0)
{
delete[] newMemory;
}
size = strlen(value);
return *this;
}

//ostream operator
ostream& operator<<(ostream& newWord, const MyString& newWord2) {

newWord << newWord2.value;
return newWord;
}


//istream operator
istream& operator >> (istream& newWord, MyString& newWord2) {

const int MAX = 100;
char* ptr = new char[MAX];
newWord >> ptr;
newWord2 = MyString(ptr);
delete ptr;
return newWord;
}

//all boolean operators
bool operator==(const MyString& newWord, const MyString& newWord2) {
if (newWord.value == newWord2.value) {
return true;
}
else {
return false;
}
}

bool operator!=(const MyString& newWord, const MyString& newWord2) {
if (newWord.value != newWord2.value) {
return true;
}
else {
return false;
}
}

bool operator<(const MyString& newWord, const MyString& newWord2) {
if (newWord.value < newWord2.value) {
return true;
}
else {
return false;
}
}

bool operator<=(const MyString& newWord, const MyString& newWord2) {
if (newWord.value <= newWord2.value) {
return true;
}
else {
return false;
}
}

bool operator>(const MyString& newWord, const MyString& newWord2) {
if (newWord.value > newWord2.value) {
return true;
}
else {
return false;
}
}

bool operator>=(const MyString& newWord, const MyString& newWord2) {
if (newWord.value >= newWord2.value) {
return true;
}
else {
return false;
}
}

//destructor to release memory
MyString::~MyString() {
delete[] value;
}

void test_copy_and_destructor(MyString S) {
cout << "test: copy constructor and destructor calls: " << endl;
MyString temp = S;
cout << "temp inside function test_copy_and_destructor: " << temp << endl;
}

int main() {

MyString st1("abc abc");
MyString st2("9fgth");

cout << "Copy constructor , << operator" << endl;

MyString st3(st1);

cout << "st3: " << st3 << endl;

test_copy_and_destructor(st2);

MyString st4;

cout << "operator + " << endl;

st4 = st3 + st2;

cout << "st4: " << st4 << endl;

cout << "st1 + st2: " << (st1 + st2) << endl;

cout << "operators [ ] " << endl;

for (int i = 0; i < st2.length(); i++)
cout << st2[i] << " ";

cout << endl;

cout << "operators += , ==, != " << endl;

st2 += st1;

if (st3 == st1)
cout << "st3 and st1 are identical " << endl;
else cout << "st3 and st1 are not identical " << endl;

if (st2 != st1)
cout << "st2 and st1 are not identical " << endl;
else cout << "st2 and st1 are identical " << endl;

cout << "operators < , <=, >, >= " << endl;

if (st2 < st1)
cout << "st2 < st1 " << endl;
else cout << "st2 is not less than st1 " << endl;

if (st1 <= st2)
cout << "st1 <= st2 " << endl;
else cout << "st1 is not less than or equal to st2 " << endl;

if (st1 > st2)
cout << "st1 > st2 " << endl;
else cout << "not (st1 > st2) " << endl;

if (st1 >= st2)
cout << "st1 >= st2 " << endl;
else cout << "not (st1 >= st2) " << endl;

cout << "operator >> " << endl;

//Open the data file
ifstream input("A9_input.txt");
if (input.fail()) {
cout << "unable to open input file A9_input.txt, Exiting..... ";
system("pause");
return 0;
}
MyString temp1;
MyString temp2("aaa");
input >> temp1;
input >> temp2;
cout << "first element of input file: " << temp1 << endl;
cout << "second element of input file: " << temp2 << endl;
input.close();

cout << "MyString says farewell....." << endl;
system("pause");
return 0;
}

最佳答案

您的复制构造函数从不设置目标的 value,因此当您尝试使用新字符串时,value 未初始化。您不应使用局部变量 newCopy,而应分配给 value

MyString::MyString(const MyString& newWord) {

//perform a deep copy to copy each of the value to a new memory
size = newWord.size;
value = new char[size];

for (int ii = 0; ii < size; ii++) {
value[ii] = newWord.value[ii];
}
}

此外,采用char* chars 的构造函数必须复制chars。否则,如果参数是超出范围的本地数组或已删除的动态数组,则指针可能会变得无效。此外,由于析构函数执行 delete[] value;,因此它需要动态分配,这在您从字符串文字初始化时是不正确的。

//constructor with an argument
MyString::MyString(char* chars) {

size = strlen(chars);
value = new char[size];
for (int i = 0; i < size; i++) {
value[i] = chars[i];
}
}

关于c++ - "Unhandled exception thrown: read access violation. _First was nullptr"错误是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38278622/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com