朋友功能不起作用(语法错误)
Friend function not working (syntax errors)
我是C 的新手,我以前从未实现过朋友功能。但是,我得到了一个特定的朋友函数,以实现如下std::ostream &operator << (std::ostream &out, const Library &lib);
。我以为应该在图书馆类的标题文件中进行,但是由于某种原因,Ostream,out和lib都在提高语法错误。我也不真正了解要成为基于此代码行的朋友是什么。抱歉,我的问题的模糊性,但是我真的对朋友功能不了解。任何帮助将不胜感激,谢谢。
library.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
library.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)
您正在尝试使该功能成为一类朋友,以便它可以访问类的私人成员。
要执行此功能,必须在类本身的定义中声明为朋友。(班级可以选择其朋友。(
因此,您需要将其放入class Library { };
:
friend std::ostream &operator << (std::ostream &out, const Library &lib);
然后您有两个选择。您实际上可以在此处定义该函数,在类定义中的内联。
另外,您可以在库中定义它。
std::ostream &operator << (std::ostream &out, const Library &lib) {
return out << lib.something << ....;
}
请注意,您不需要 - 确实不能 - 使用friend
关键字。那已经得到了照顾。
相关文章:
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 为什么我会收到错误 C2143 语法错误:缺少"*"之前的';'?
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 我该怎么做才能摆脱使用命名空间时的语法错误?
- C++语法头文件错误
- 如何摆脱错误c2061;语法错误 cout?
- 语法错误:D3DX10Core 中的标识符'TEXTMETRICA'
- 错误 C2146:语法错误:标识符"A1"之前缺少","
- main.cpp(11):错误 C2059:语法错误:"字符串"
- 我想获取点的属性,它报告错误 C3867:"point::output_x":非标准语法;使用"&"创建指向成员的指针
- RedisGraph 语法错误在偏移量 8 靠近"创建"
- 如何修复此 FSM C++语法错误 C3867
- 函数错误 C2059:语法错误:'>'不起作用
- 为什么在main()之前断言会导致语法错误"error: expected ')' before numeric constant"?
- system() 返回错误语法错误:"("意外
- C++图形初始化错误(语法或丢失文件缺陷?
- 获取0:1(1):错误:语法错误,尝试编译着色器时意外$end
- Visual Studio错误:语法错误:标识符