我的代码中出现了大量错误,这一切都归结为以下几点:
Library.cpp:89:错误:'((Library *)this)中成员'getLocation'的请求 - > Library :: holdings.std :: vector< _Tp,_Alloc> :: operator [] [with _Tp = Book *,_ Alloc = std :: allocator]((long unsigned int)bookOnFile))',它是非类型'Book *'
我对如何编写以下内容感到困惑:
Patron* matchPatron = &members[patronOnFile];
if (PatronIDMatch == true && bookIDMatch == true) {
if (holdings[bookOnFile].getLocation() == ON_SHELF) {
holdings[bookOnFile].setCheckedOutBy(matchPatron);
holdings[bookOnFile].setLocation(CHECKED_OUT);
holdings[bookOnFile].setDateCheckedOut(currentDate);
members[patronOnFile].setCheckedOutBooks(&holdings[bookOnFile]);
cout << members[patronOnFile].getName() << " check out successful"
<< holdings[bookOnFile].getTitle() << ".";
(我有这么多行的错误代码,这个只适用于getLocation行。)
我的标题如下:
//Library.hpp
#ifndef LIBRARY_HPP
#define LIBRARY_HPP
#include <string>
#include <vector>
#include "Patron.hpp"
class Library {
private:
std::vector<Book*> holdings;
std::vector<Patron*> members;
int currentDate;
public:
Library();
void addBook(Book*);
void addPatron(Patron*);
std::string checkOutBook(std::string pID, std::string bID);
std::string returnBook(std::string bID);
std::string requestBook(std::string pID, std::string bID);
std::string payFine(std::string pID, double payment);
void incrementCurrentDate();
Patron* getPatron(std::string pID);
Book* getBook(std::string bID);
};
#endif
我在这里写错了代码吗?我该怎么写呢?如果需要,我可以提供我的整个程序。
编辑:
Patron* matchPatron = &members[patronOnFile];
答案 0 :(得分:1)
你有一个Book*
的向量,它们是指针,但你在调用holdings[bookOnFile].getLocation()
时使用的是点符号。其他方法调用也是如此。尝试替换为holdings[bookOnFile]->getLocation()
等。