これが私のメインです:
#include <iostream>
#include <vector>
#include "Book.h"
using namespace std;
vector<Book> removeDuplicates(vector<Book> oldbookvector)
{
vector<Book> newbookvector;
vector<string> booknames;
for(vector<Book>::size_type i = 0; i < oldbookvector.size(); i ++){
booknames.push_back(oldbookvector[i].bookname());
}
vector<Book>::iterator it;
for(vector<Book>::size_type i = 0; i < oldbookvector.size(); i ++){
//If it find a value then it returns the first element
it = find(newbookvector.begin(),newbookvector.end(), oldbookvector[i]);
if(it == newbookvector.end()){
booknames.push_back(oldbookvector[i].bookname());
}
}
return newbookvector;
}
int main(int argc, const char * argv[])
{
vector<Book> books;
vector<Book> newbooks;
books.push_back(Book("The C Programming Language", 1980));
books.push_back(Book("Javascript: The Good Parts", 2008));
books.push_back(Book("Accelerated C++: Pratical Programming by Example", 2000));
books.push_back(Book("Scala for the Impatient", 2012));
books.push_back(Book("The C Programming Language", 1980));
books.push_back(Book("Javascript: The Good Parts", 2008));
books.push_back(Book("Accelerated C++: Pratical Programming by Example", 2000));
books.push_back(Book("Scala for the Impatient", 2012));
cout << &books[2] << endl;
cout << books[2].bookname() << endl;
//Test to make sure book class was working
Book stuff = Book("The Book about stuff", 1998);
cout << stuff.bookname() << endl;
cout << stuff.bookyear() << endl;
stuff.printBook();
//newbooks = removeDuplicates(books);
//Print out the vector without duplicates
for(vector<Book>::size_type i = 0; i < newbooks.size(); i ++){
cout << newbooks[i].bookname() << newbooks[i].bookyear() << "\t";
}
return 0;
}
algorithm.cc で、「バイナリ式 ('Book' および 'const Book') のオペランドが無効です」というエラーが表示され続けます。find() から来ていると思います。問題は、イテレータが本を繰り返し処理していないことだと確信しています
ここに私の本のクラスがあります
#ifndef Question2_Book_h
#define Question2_Book_h
using namespace std;
class Book{
public:
Book();
Book(string n, int y){
name = n;
year = y;
}
string bookname(){
return name;
}
int bookyear(){
return year;
}
void printBook(){
cout << name << ", " << year << endl;
}
private:
string name;
int year;
};
#endif