Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对“librarymanager::getBook”的未定义引用(标准::基本字符串<;字符,标准::字符特征<;字符>;,标准::分配器<;字符>;)_C++_String - Fatal编程技术网

C++ 对“librarymanager::getBook”的未定义引用(标准::基本字符串<;字符,标准::字符特征<;字符>;,标准::分配器<;字符>;)

C++ 对“librarymanager::getBook”的未定义引用(标准::基本字符串<;字符,标准::字符特征<;字符>;,标准::分配器<;字符>;),c++,string,C++,String,嗨,我面临着“对`librarymanager::getBook(std::basic_string,std::allocator>)的未定义引用”的问题,尽管我花了很多时间来解决这个问题。我的代码如下 图书馆经理 #ifndef LIBRARYMANAGER_H #define LIBRARYMANAGER_H #include <string> #include "Book.h" namespace librarymanager { class LibraryManage

嗨,我面临着“对`librarymanager::getBook(std::basic_string,std::allocator>)的未定义引用”的问题,尽管我花了很多时间来解决这个问题。我的代码如下

图书馆经理

#ifndef LIBRARYMANAGER_H
#define LIBRARYMANAGER_H
#include <string>
#include "Book.h"

 namespace librarymanager {
  class LibraryManager {
  public:
     void addBook(const book::Book& book);
     void removeBook(std::string bookName);
     void markBarrowed(std::string bookName, std::string borrower);
     void printAllBooks();
     void printAllBorrowedBooks();
     void printAllBorrowerDetails();
  };

  book::Book& getBook(std::string bookName);

 }
#endif 
#ifndef LIBRARYMANAGER_H
#define LIBRARYMANAGER_H

#include <string>
#include <map>

#include "Book.h"

 namespace librarymanager {

  class LibraryManager {
  public:
     void addBook(const book::Book& book);
     void removeBook(std::string bookName);
     void markBarrowed(std::string bookName, std::string borrower);
     void printAllBooks();
     void printAllBorrowedBooks();
     void printAllBorrowerDetails();

  private:
      typedef std::map<std::string, book::Book> Books;

      Books shelf;
  };

  book::Book getBook(std::string bookName);

 }

#endif 
Book.cpp

#include <iostream>
using namespace librarymanager;
LibraryManager::class Book {
  private:
     string bookName;
     bool borrowed;
     string borrower;
  public:
    Book(string bookName) {
        this->bookName = bookName;
    }

    string getBookName() {
        return bookName;
    }

    void setBorrowed(bool borrowed, string borrower) {
        if (borrowed == true) {
            this->borrowed = borrowed;
            this->borrower = borrower;  
        }
    }

    bool  isBorrowed() {
        return borrowed;
    }

    string getBorrower() {
        return borrower;
    }
  };
#include <iostream>

#include "Book.h"

using namespace book;

    Book::Book(std::string bookName) {
        this->bookName = bookName;
    }

    std::string Book::getBookName() const {
        return bookName;
    }

    void Book::setBorrowed(bool borrowed, std::string borrower) {
        if (borrowed == true) {
            this->borrowed = borrowed;
            this->borrower = borrower;  
        }
    }

    bool  Book::isBorrowed() {
        return borrowed;
    }

    std::string Book::getBorrower() {
        return borrower;
    }
#包括
使用名称空间库管理器;
图书馆管理员::课程手册{
私人:
字符串书名;
布尔借来的;
借款者;
公众:
图书(字符串图书名称){
此->书名=书名;
}
字符串getBookName(){
返回书名;
}
无效借项(bool借项、字符串借项){
如果(借用==真){
本->借用=借用;
该->借款人=借款人;
}
}
布尔是借用的{
归还借款;
}
字符串getbooker(){
归还借款人;
}
};
LibraryManagerTest.cpp

#include <iostream>
#include "LibraryManager.h"
#include "Book.h"
using namespace std;
using namespace librarymanager;
using namespace book;

int main() {
int operation = -1;
LibraryManager libraryManager;
while(true) {
    cout << "Select operation " << endl;
    cout << "1. Add Book" << endl;
    cout << "2. Remove Book" << endl;
    cout << "3. Borrow Book" << endl;
    cout << "4. Print All Books " << endl;
    cin >> operation;

    switch (operation) {
        case 1:
            cout << "Enter the name of the book " << endl;
            string bookName;
            getline(cin, bookName);
            book::Book& newBook = getBook(bookName);
            libraryManager.addBook(newBook);
            break;
        /*case 2:
            break;

        case 3:
            break;

        case 4:
            break;

        default:
            break;*/

    }
}

}
#include <iostream>
#include "LibraryManager.h"
#include "Book.h"
using namespace std;
using namespace librarymanager;
using namespace book;

int main() {
int operation = -1;
LibraryManager libraryManager;
while(true) {
    cout << "Select operation " << endl;
    cout << "1. Add Book" << endl;
    cout << "2. Remove Book" << endl;
    cout << "3. Borrow Book" << endl;
    cout << "4. Print All Books " << endl;
    cin >> operation;

    switch (operation) {
        case 1:
            cout << "Enter the name of the book " << endl;
            string bookName;
            getline(cin, bookName);
            book::Book& newBook = getBook(bookName);
            libraryManager.addBook(newBook);
            break;
        /*case 2:
            break;

        case 3:
            break;

        case 4:
            break;

        default:
            break;*/

    }
}

}
#包括
#包括“LibraryManager.h”
#包括“Book.h”
使用名称空间std;
使用名称空间库管理器;
使用名称空间书;
int main(){
整数运算=-1;
图书馆经理图书馆经理;
while(true){

cout如果您声明了头文件

void addBook(const book::Book& book);
但在.cpp中,您称之为
addBooks

LibraryManager::void addBooks(const Books& book) {
    shelf.insert(book.getName(), book);
}

所以您没有实现
addBook
。这就是为什么在调用
addBook
(如果您声明了头文件)时,main中会出现链接器错误的原因

void addBook(const book::Book& book);
但在.cpp中,您称之为
addBooks

LibraryManager::void addBooks(const Books& book) {
    shelf.insert(book.getName(), book);
}

所以您没有实现
addBook
。这就是为什么在调用
addBook
(如果您声明了头文件)时,main中会出现链接器错误的原因

void addBook(const book::Book& book);
但在.cpp中,您称之为
addBooks

LibraryManager::void addBooks(const Books& book) {
    shelf.insert(book.getName(), book);
}

所以您没有实现
addBook
。这就是为什么在调用
addBook
(如果您声明了头文件)时,main中会出现链接器错误的原因

void addBook(const book::Book& book);
但在.cpp中,您称之为
addBooks

LibraryManager::void addBooks(const Books& book) {
    shelf.insert(book.getName(), book);
}

所以您没有实现
addBook
。这就是为什么在调用
addBook
时,在LibraryManager.cpp文件中没有定义
void addBook(const book::book&book);
函数,在main中出现链接器错误的原因

在Book.h和Book.cpp中有不同的名称空间…因此您在.h文件中声明了另一本书,并在.cpp文件中定义了新的一本书…我想是因为您忘了在Book.cpp中添加
#include“Book.h”

更新

我最终重写了你的代码…犯了很多错误。没有使用常量、加扰的代码。写东西时请编译更多:

#ifndef BOOK_H
#define BOOK_H
namespace book {
    class Book{
    public:
        void getBookName();
        void setBorrowed(bool borrowed, std::string borrower);
        bool isBorrowed();
        std::string getBorrower();
    };
}
#endif
#ifndef BOOK_H
#define BOOK_H
namespace book {
    class Book {
    public:
        Book(std::string bookName);

        std::string getBookName() const;
        void setBorrowed(bool borrowed, std::string borrower);
        bool isBorrowed();
        std::string getBorrower();

    private:

     std::string bookName;
     bool borrowed;
     std::string borrower;
    };
}
#endif
Book.cpp

#include <iostream>
using namespace librarymanager;
LibraryManager::class Book {
  private:
     string bookName;
     bool borrowed;
     string borrower;
  public:
    Book(string bookName) {
        this->bookName = bookName;
    }

    string getBookName() {
        return bookName;
    }

    void setBorrowed(bool borrowed, string borrower) {
        if (borrowed == true) {
            this->borrowed = borrowed;
            this->borrower = borrower;  
        }
    }

    bool  isBorrowed() {
        return borrowed;
    }

    string getBorrower() {
        return borrower;
    }
  };
#include <iostream>

#include "Book.h"

using namespace book;

    Book::Book(std::string bookName) {
        this->bookName = bookName;
    }

    std::string Book::getBookName() const {
        return bookName;
    }

    void Book::setBorrowed(bool borrowed, std::string borrower) {
        if (borrowed == true) {
            this->borrowed = borrowed;
            this->borrower = borrower;  
        }
    }

    bool  Book::isBorrowed() {
        return borrowed;
    }

    std::string Book::getBorrower() {
        return borrower;
    }
#包括
#包括“Book.h”
使用名称空间书;
Book::Book(std::string bookName){
此->书名=书名;
}
std::string Book::getBookName()常量{
返回书名;
}
作废帐簿::setBorrowed(bool借阅,std::字符串借阅){
如果(借用==真){
本->借用=借用;
该->借款人=借款人;
}
}
bool Book::islowed(){
归还借款;
}
std::string Book::get借款人(){
归还借款人;
}
图书馆经理

#ifndef LIBRARYMANAGER_H
#define LIBRARYMANAGER_H
#include <string>
#include "Book.h"

 namespace librarymanager {
  class LibraryManager {
  public:
     void addBook(const book::Book& book);
     void removeBook(std::string bookName);
     void markBarrowed(std::string bookName, std::string borrower);
     void printAllBooks();
     void printAllBorrowedBooks();
     void printAllBorrowerDetails();
  };

  book::Book& getBook(std::string bookName);

 }
#endif 
#ifndef LIBRARYMANAGER_H
#define LIBRARYMANAGER_H

#include <string>
#include <map>

#include "Book.h"

 namespace librarymanager {

  class LibraryManager {
  public:
     void addBook(const book::Book& book);
     void removeBook(std::string bookName);
     void markBarrowed(std::string bookName, std::string borrower);
     void printAllBooks();
     void printAllBorrowedBooks();
     void printAllBorrowerDetails();

  private:
      typedef std::map<std::string, book::Book> Books;

      Books shelf;
  };

  book::Book getBook(std::string bookName);

 }

#endif 
\ifndef图书馆经理
#定义图书馆经理
#包括
#包括
#包括“Book.h”
命名空间库管理器{
班级图书馆经理{
公众:
无效addBook(const book::book&book);
void removeBook(std::string bookName);
作废标记(std::string bookName,std::string借用者);
作废printAllBooks();
作废打印所有借阅图书();
void printallowerdetails();
私人:
typedef std::地图册;
书架;
};
book::book getBook(std::string bookName);
}
#恩迪夫
LibraryManager.cpp

#include <iostream>
#include "LibraryManager.h"

#include "Book.h"
#include <map>
using namespace std;

map<string, Books&> shelf;

LibraryManager::void addBooks(const Books& book) {
    shelf.insert(book.getName(), book);
}

LibraryManager::void removeBook(string bookName) {
    shelf.erase(bookName);
}

LibraryManager::void printAll() {
    for(map<string, Book&>::const_iterator iter = shelf.begin(), endIter = shelf.end(); iter != endIter(); iter++) {
        cout << iter->first << endl;
    }
}
namespace librarymanager {
    book::Book& getBook(string name) {
        Book book(name);
        return book;
    }
}
#include <iostream>

#include "LibraryManager.h"

#include "Book.h"

using namespace librarymanager;
using namespace book;
using namespace std;

void LibraryManager::addBook(const Book& book) {
    shelf.insert(std::pair<std::string, book::Book>(book.getBookName(), book));
}

void LibraryManager::removeBook(string bookName) {
    shelf.erase(bookName);
}

void LibraryManager::printAllBooks() {
    for(map<string, Book>::const_iterator iter = shelf.begin(); iter != shelf.end(); iter++) {
        cout << iter->first << endl;
    }
}

namespace librarymanager {
    book::Book getBook(string name) {
        Book book(name);
        return book;
    }
}
#包括
#包括“LibraryManager.h”
#包括“Book.h”
使用名称空间库管理器;
使用名称空间书;
使用名称空间std;
void LibraryManager::addBook(const Book和Book){
insert(std::pair(book.getBookName(),book));
}
作废LibraryManager::removeBook(字符串bookName){
书架。擦除(书名);
}
void LibraryManager::printAllBooks(){
for(map::const_iterator iter=shelf.begin();iter!=shelf.end();iter++){

不能先在LibraryManager.cpp文件中定义
void addBook(const book::book&book);
函数

在Book.h和Book.cpp中有不同的名称空间…因此您在.h文件中声明了另一本书,并在.cpp文件中定义了新的一本书…我想是因为您忘了在Book.cpp中添加
#include“Book.h”

更新

我最终重写了你的代码…犯了很多错误。没有使用常量、加扰的代码。写东西时请编译更多:

#ifndef BOOK_H
#define BOOK_H
namespace book {
    class Book{
    public:
        void getBookName();
        void setBorrowed(bool borrowed, std::string borrower);
        bool isBorrowed();
        std::string getBorrower();
    };
}
#endif
#ifndef BOOK_H
#define BOOK_H
namespace book {
    class Book {
    public:
        Book(std::string bookName);

        std::string getBookName() const;
        void setBorrowed(bool borrowed, std::string borrower);
        bool isBorrowed();
        std::string getBorrower();

    private:

     std::string bookName;
     bool borrowed;
     std::string borrower;
    };
}
#endif
Book.cpp

#include <iostream>
using namespace librarymanager;
LibraryManager::class Book {
  private:
     string bookName;
     bool borrowed;
     string borrower;
  public:
    Book(string bookName) {
        this->bookName = bookName;
    }

    string getBookName() {
        return bookName;
    }

    void setBorrowed(bool borrowed, string borrower) {
        if (borrowed == true) {
            this->borrowed = borrowed;
            this->borrower = borrower;  
        }
    }

    bool  isBorrowed() {
        return borrowed;
    }

    string getBorrower() {
        return borrower;
    }
  };
#include <iostream>

#include "Book.h"

using namespace book;

    Book::Book(std::string bookName) {
        this->bookName = bookName;
    }

    std::string Book::getBookName() const {
        return bookName;
    }

    void Book::setBorrowed(bool borrowed, std::string borrower) {
        if (borrowed == true) {
            this->borrowed = borrowed;
            this->borrower = borrower;  
        }
    }

    bool  Book::isBorrowed() {
        return borrowed;
    }

    std::string Book::getBorrower() {
        return borrower;
    }
#包括
#包括“Book.h”
使用名称空间书;
Book::Book(std::string bookName){
此->书名=书名;
}
std::string Book::getBookName()常量{
返回书名;
}
作废帐簿::setBorrowed(bool借阅,std::字符串借阅){
如果(借用==真){
本->借用=借用;
该->借款人=借款人;
}
}
bool Book::islowed(){
归还借款;
}
std::string Book::get借款人(){
归还借款人;
}
图书馆经理

#ifndef LIBRARYMANAGER_H
#define LIBRARYMANAGER_H
#include <string>
#include "Book.h"

 namespace librarymanager {
  class LibraryManager {
  public:
     void addBook(const book::Book& book);
     void removeBook(std::string bookName);
     void markBarrowed(std::string bookName, std::string borrower);
     void printAllBooks();
     void printAllBorrowedBooks();
     void printAllBorrowerDetails();
  };

  book::Book& getBook(std::string bookName);

 }
#endif 
#ifndef LIBRARYMANAGER_H
#define LIBRARYMANAGER_H

#include <string>
#include <map>

#include "Book.h"

 namespace librarymanager {

  class LibraryManager {
  public:
     void addBook(const book::Book& book);
     void removeBook(std::string bookName);
     void markBarrowed(std::string bookName, std::string borrower);
     void printAllBooks();
     void printAllBorrowedBooks();
     void printAllBorrowerDetails();

  private:
      typedef std::map<std::string, book::Book> Books;

      Books shelf;
  };

  book::Book getBook(std::string bookName);

 }

#endif 
\ifndef图书馆经理
#定义图书馆经理
#包括
#包括
#包括“Book.h”
命名空间库管理器{
班级图书馆经理{
公众:
无效addBook(const book::book&book);
void removeBook(std::string bookName);
作废标记(std::string bookName,std::string借用者);
作废printAllBooks();
作废打印所有借阅图书();
void printallowerdetails();
私人:
typedef std::地图册;
书架;
};
book::book getBook(std::string bookName);
}
#e