在C++中为类分配内存

Kni*_*tex 1 c++ memory class vector

这是我的班级:

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 */
Run Code Online (Sandbox Code Playgroud)

以下是它的来源:

#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;
}
Run Code Online (Sandbox Code Playgroud)

这是它运行的主文件:

    #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;
}
Run Code Online (Sandbox Code Playgroud)

我跑的时候一直这样:

bookclass(58316)malloc:*对象0x7fff522d78b0的错误:未释放指针被释放*在malloc_error_break中设置断点以进行调试

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

Die*_*ühl 6

成员位于对象内部,即,为它们分配Book对象的内存,并且既不delete明确也不需要内存.基本上,您需要使用new调用来匹配您的显式分配,delete但您永远不需要释放未明确分配的内容.

也就是说,当您尝试delete title或时,您会收到错误delete year.当尝试delete authors依赖于authors来自哪里时,也可能发生这种情况.通常,您不希望delete未分配的对象.你的Book班级可能不合理地取得了authors矢量的所有权.