运算符重载 - 与'operator!='不匹配(操作数类型为

时间:2016-09-02 17:51:07

标签: c++ operator-overloading

我是一个相当新的/在大学一级学习C ++的过程中,在我的一个任务中我一直得到一个我不知道如何解决的错误,如果你退伍军人和/或我会很感激天才可以帮助我/推动我朝着正确的方向前进。

我似乎在我的类函数的实现中遇到了问题,特别是运算符重载。 以下是我的头文件:

#ifndef LIBRARY_H
#define LIBRARY_H

#include "book.h"
#include <iostream>
#include <string>

using namespace std;

class Library;

class Library
{
    private:
        string name;
        Book** books;
        int librarySize;
        int numBooks;
    public:
        //Constructor, Copy constructor and Destructor
        Library(string a);
        Library(const Library& obj);
        ~Library();

        //Operator overloads
        Library& operator+=( Book right);
        Library& operator-=(Book right);
        Library& operator=(const Library &right);
        Library& operator++(int);
        Library& operator--();

        //Accessors
        Book* getBook(string);
        bool isFull();
        void print();



};

#endif

这个错误似乎来自我的.cpp文件:

#include"library.h"
#include "book.h"
#include <iostream>
#include <string>



 Library::Library(string a)
{
    name = a;
}

Library::Library(const Library& obj)
{

    name = obj.name;
    librarySize = obj.librarySize;
    numBooks = obj.numBooks;

    //Copy Books* array
    for(int x=0; x!= librarySize; x++)
        *(books+x) = *(obj.books+x);

}
//Operator overloads
Library& Library::operator+=( Book right)
{

    if(numBooks == librarySize)
         cout<<"Library is full!\n";
    else
    {
        numBooks++;     
        *books[numBooks] = right;
    }
    return *this;

}



Library& Library::operator-=(Book right)
{
    if(numBooks != 0)
    {
        for(int x=0; x!= numBooks; x++)
        {
            if(*books[x] != right){}
            else
                *books[x] = "";
        }
    }
    else
        cout<<"Library is empty!\n";
    return *this;
}

Library& Library::operator=(const Library &right)
{

    name = right.name;
    librarySize = right.librarySize;
    numBooks = right.numBooks;
    for(int x=0; x!= librarySize; x++)
        *(books+x) = *(right.books+x);
    return *this;
}

Library& Library::operator++(int)
{

    librarySize++;
    return *this;

}

Library& Library::operator--()
{

    if(numBooks == librarySize)
    {   
        *books[numBooks] = right;
        numBooks--;
    }
    librarySize--;
    return *this;
}

我认为这是一个错误,然后是大量的“注释:”s

root@kali:~/Desktop/Prac3# g++ -std=c++11 -c library.cpp library.h
library.cpp: In member function ‘Library& Library::operator-=(Book)’:
library.cpp:48:17: error: no match for ‘operator!=’ (operand types are ‘Book’ and ‘Book’)

自从过去2天以来,我一直在寻找答案,但我的教授和互联网都没有给我一个解决方案。我也尝试了不同的方法来做同样的事情但我得到了其他错误以及这个。虽然看起来当我删除“ - =”重载时它并没有给我所有的注释,它只是给了我上面的错误。

提前致谢。

(我对整个'博客'例程都很陌生,所以如果我犯了任何netiqette错误,或者如果你需要更多信息请在没有坏语言的情况下进行评论:)

1 个答案:

答案 0 :(得分:1)

在 - =运算符重载实现中,您要进行比较,看看是否有两本书!=。看来编译器找不到任何这样的实现,所以我假设你没有为Book编写你的!=或==重载。