gpt4 book ai didi

c++ - 在C++中为类分配内存

转载 作者:太空宇宙 更新时间:2023-11-03 10:32:24 25 4
gpt4 key购买 nike

这是我的课:

using namespace std;

Class Book {
public:
Book();
Book(vector<string>*, string, int);
Book(const Book&);
~Book();
Book& operator=(const Book&);
void update(vector<string>*);
void update(string);
void update(int);
int getYear() const{
return year;
};
string getTitle() const{
return title;
};
bool operator==(const Book&);
bool operator!=(const Book&);
friend std::ostream& operator<<(std::ostream&, const Book&);
void getAuthors();
private:
vector<string>* authors;
string title;
int year;
};

#endif /* BOOK_H */

这是它的来源:

#include "Book.h"
using namespace std;

Book::Book():year(0), title(NULL), authors(NULL){}
Book::Book(vector<string>* bookauthors,string booktitle, int bookyear ){
authors = bookauthors;
title = booktitle;
year = bookyear;
}

Book::Book(const Book& aBook){
authors = aBook.authors;
title = aBook.title;
year = aBook.year;
}

Book::~Book(){
delete authors;
delete &title;
delete &year;
}

bool Book::operator==(const Book &aBook){
if(getYear() == aBook.getYear() && getTitle() == aBook.getTitle())
return true;
else return false;
}

bool Book::operator != (const Book &aBook){
if(getYear() != aBook.getYear() && getTitle() != aBook.getTitle())
return true;
else return false;
}

Book& Book::operator =(const Book& rhs){
if(this != &rhs){
authors = rhs.authors;
title = rhs.title;
year = rhs.year;
}
return *this;
}

void Book::update(int newyear){
year = newyear;
}

void Book::update(string newtitle){
title = newtitle;
}

void Book::update(vector<string>* newauthors){
authors = newauthors;
}

std::ostream& operator <<(std::ostream& os, const Book& b){
os<<b.getTitle()<<", "<<b.getYear();
return os;
}

这是它运行的主要文件:

    #include "Book.h"
#include <iostream>
#include <limits.h>
//This is the test funcion posted on the class website
using namespace std;

int main(){

//testing constructor
vector<string> authors;
authors.push_back("Ritchie");
authors.push_back("Kernighan");
Book a(&authors, "C", 1990);
authors.push_back("Whatever");
cout << "Book a is: " << a << endl;
cout << "Expected: (C, 1990, Ritchie & Kernighan)" << endl;

//testing copy constructor
Book b(a);
a.update(&authors);
cout << "Book b is: " << b << endl;
cout << "Expected: (C, 1990, Ritchie & Kernighan)" << endl;

//testing constructor
vector<string> authors2;
authors2.push_back("Crockford");
Book c(&authors2, "JavaScript", 2008);
cout << "Book c is: " << c << endl;
cout << "Expected: (JavaScipt, 2008, Crockford)" << endl;

//testing assignment operator
authors2.push_back("whatever");
a=c;
cout << "Book a is changed to: " << a << endl;
cout << "Expected: (JavaScipt, 2008, Crockford)" << endl;

for(int i=0; i < 200000000; i++)
b=c;
cout << "Book b is changed to: " << b << endl;
cout << "Expected: (JavaScipt, 2008, Crockford)" << endl;
}

当我运行它时,我一直得到这个:

bookclass(58316) malloc: * error for object 0x7fff522d78b0: pointer being freed was not allocated* set a breakpoint in malloc_error_break to debug

我是 C++ 的新手,所以我不确定如何分配内存。我尝试使用 malloc 但它没有用。

最佳答案

成员位于对象内部,即它们的内存是通过 Book 对象分配的,既不能也不需要显式地删除内存。基本上,您需要将使用 new 的显式分配与对 delete 的调用相匹配,但您永远不需要释放未在某处显式分配的内容。

也就是说,当您尝试delete titledelete year 时出现错误。根据 authors 的来源,尝试 delete authors 时也可能发生这种情况。通常,您不想删除 尚未分配的对象。您的 Book 类可能不合理地取得了 authors vector 的所有权。

关于c++ - 在C++中为类分配内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12905102/

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