Я новичок в С++, я никогда раньше не реализовывал функцию друга. Но мне дали конкретную функцию друга для реализации, которая выглядит следующим образом std::ostream &operator << (std::ostream &out, const Library &lib);
. Я предположил, что это должно было быть в заголовочном файле для класса Library, но по какой-то причине ostream, out и lib вызывают синтаксические ошибки. Я также не очень понимаю, что пытается быть другом того, что основано на этой строке кода. Извините за расплывчатость моего вопроса, но опять же, я действительно мало знаю о функциях друзей. Любая помощь будет оценена, спасибо.
Библиотека.h
#include <iostream>
#ifndef ASS1_LIBRARY_H
#define ASS1_LIBRARY_H
class Library {
private:
static const int MAX = 10;
std::string BookList[MAX];
std::string libraryName;
int length;
public:
explicit Library(const std::string &name);
// Add a new book,
// return true for success, false if book already in library
bool AddBook(const std::string &name);
// Remove a book
// return true for success, false if book not in library
bool RemoveBook(const std::string &name);
// List all books in library
void ListAllBooks() const;
// Return true if book in library, false otherwise
bool IsInLibrary(const std::string &name) const;
std::string getLibraryName();
};
//friend function
std::ostream &operator << (std::ostream &out, const Library &lib);
#endif //ASS1_LIBRARY_H
Библиотека.cpp
#include <iostream>
#include "Library.h"
using namespace std;
Library::Library(const string &name) {
libraryName = name;
length = 0;
}
bool Library::AddBook(const string &name) {
if(IsInLibrary(name) || length >= MAX) {
return false;
}
else {
length++;
BookList[length - 1] = name;
return true;
}
}
bool Library::RemoveBook(const std::string &name) {
int counter = 0;
while(counter < length) {
if(BookList[counter] == name) {
BookList[counter] = "";
length--;
while(counter < length) {
BookList[counter] = BookList[counter + 1];
BookList[counter + 1] = "";
counter++;
}
return true;
}
counter++;
}
return false;
}
void Library::ListAllBooks() const {
int counter = 0;
while(counter < length) {
cout << BookList[counter];
if(counter != length - 1) {
cout << "," << endl;
}
counter++;
}
cout << endl;
}
bool Library::IsInLibrary(const std::string &name) const {
int counter = 0;
while(counter < length) {
if(BookList[counter] == name) {
return true;
}
counter++;
}
return false;
}
string Library::getLibraryName() {
return libraryName;
}
main.cpp
#include <iostream>
#include "Library.h"
int main() {
Library execute("MyLibrary");
execute.AddBook("Book 1");
execute.AddBook("Book 2");
execute.AddBook("Book 3");
execute.AddBook("Book 4");
execute.AddBook("Book 5");
execute.AddBook("Book 6");
execute.AddBook("Book 7");
execute.AddBook("Book 8");
execute.AddBook("Book 9");
execute.AddBook("Book 10");
execute.RemoveBook("Book 3");
execute.RemoveBook("Book 5");
execute.RemoveBook("Book 10");
execute.RemoveBook("B");
execute.ListAllBooks();
std::cout << execute.getLibraryName() << std::endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.9)
project(ass1)
set(CMAKE_CXX_STANDARD 14)
add_executable(ass1 main.cpp Library.cpp Library.h)
Library
, что существует функция друга. - person spectras   schedule 01.04.2018operator<<(ostream & out, const Foo & obj)
была определена прямо внутри заголовка, возможно, с квалификациейinline
и просто{ obj.print(out); return out; }
. При фактической работе в обычномprint
методе. - person spectras   schedule 01.04.2018