1

这是我的课:

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: * 对象 0x7fff522d78b0 的错误:未分配被释放的指针 *在 malloc_error_break 中设置断点以进行调试

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

4

2 回答 2

6

成员位于对象内部,即它们的内存是由Book对象分配的,既不能也不需要delete显式地分配内存。new基本上,您需要使用with 调用来匹配您的显式分配,delete但您永远不需要释放未在某处显式分配的东西。

也就是说,当您尝试delete title或时会出现错误delete year。尝试delete authors取决于来自哪里时,也可能发生这种情况authors。通常,您不想delete分配尚未分配的对象。您的班级可能不合理地获得了向量Book的所有权。authors

于 2012-10-15T22:55:38.270 回答
2

这可能是学习使用 Valgrind 的好时机,它将为您提供更丰富的调试工具来解决此类错误。

在您的析构函数中,您正在通过指针销毁标题和年份。你实际上不需要这样做,因为它们是静态分配的(即你没有使用 new 创建它们),所以它抱怨你试图删除你没有动态创建的东西。

此外,您正在删除您的std::vector,它可以引用std::vector另一个类中包含的相同内容。由于您可能有两个包含相同引用的类,因此您需要找到一种更智能的方法来删除它,这样您就不会调用双重释放。

于 2012-10-15T22:55:35.337 回答