gpt4 book ai didi

c++ - friend 功能不工作(语法错误)

转载 作者:行者123 更新时间:2023-11-30 05:02:59 25 4
gpt4 key购买 nike

我是 C++ 的新手,我以前从未实现过友元函数。但是我得到了一个特定的 friend 函数来实现,如下所示 std​::​ostream &​operator ​<< (​std​::​ostream ​&out, ​const ​Library ​&lib); .我假设这应该放在 Library 类的头文件中,但由于某种原因,ostream、out 和 lib 都引发了语法错误。我也不太明白是什么试图成为基于这行代码的 friend 。很抱歉我的问题含糊不清,但我真的不太了解 friend 功能。任何帮助将不胜感激,谢谢。

库.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;
}

主要.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)

最佳答案

您正在尝试使该函数成为该类的友元,以便它可以访问该类的私有(private)成员。

要做到这一点,必须在类本身的定义中将函数声明为友元。 (类(class)可以选择它的 friend 。)

因此您需要将其放入 class Library { };:

friend std​::​ostream &​operator ​<< (​std​::​ostream ​&out, ​const ​Library ​&lib);

那么你有两个选择。您实际上可以在那里定义函数,在类定义中内联。

或者,更接近您的风格,您可以在 Library.cpp 中定义它,如下所示:

std​::​ostream &​operator ​<< (​std​::​ostream ​&out, ​const ​Library ​&lib) {
return out << lib.something << ....;
}

请注意,在这里您不需要 - 实际上不能 - 使用 friend 关键字。这已经解决了。

关于c++ - friend 功能不工作(语法错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49596738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com