【发布时间】:2016-07-10 04:17:11
【问题描述】:
好的,这是我的代码:
#include "driver.h"
#include "stdafx.h"
#include "Book.h"
#include "Author.h"
#include <vector>
void displayBooks(const vector<Book>& books)
{
// students need to write the code for this function
for (int i = 0; i < &books.size; i++)
{
std::cout << &books[i] << std::endl;
}
}
int main()
{
// create a vector for storing the account objects
vector<Book> myBooks;
// create three Author objects
Author p1("J.K.Rowling", "Edinburgh, Scotland");
Author p2("Suzanne Collins", "Connecticut, USA");
Author p3("J.R.R. Tolkien", "Bournmouth, England");
// Create three Book objects
Book b1(p1, "Harry Potter and the Sorcerer's Stone", 256, 24.95);
Book b2(p2, "Mockingjay", 400, 12.99);
Book b3(p3, "The Hobbit", 322, 14.29);
// add the books to the vector
myBooks.push_back(b1);
myBooks.push_back(b2);
myBooks.push_back(b3);
// call the displayBooks function to display the books
displayBooks(myBooks);
cout << "\n\n";
system("PAUSE");
return 0;
}
最上面的部分是给我带来麻烦的 displayBooks 功能。我不断得到一个 '&' 对绑定成员函数表达式的非法操作 错误。我该如何解决这个问题?
【问题讨论】:
-
i < books.size()可能还有std::cout << books[i],假设Book提供了一个合适的operator<<过载。
标签: c++