簡體   English   中英

未定義對`librarymanager :: getBook(std :: basic_string的引用 <char, std::char_traits<char> ,std :: allocator <char> &gt;)

[英]undefined reference to `librarymanager::getBook(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)

嗨,盡管我花了很多時間來解決問題,但我仍面臨“未定義對`librarymanager :: getBook(std :: basic_string,std :: allocator>)的引用”的問題。 我的代碼如下。

LibraryManager.h

#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 

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

書本

#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

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

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;*/

    }
}

}

錯誤是

/tmp/cc60p3k8.o: In function `main':
LibraryManagerTest.cpp:(.text+0x10e): undefined reference to `librarymanager::getBook(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
LibraryManagerTest.cpp:(.text+0x131): undefined reference to `librarymanager::LibraryManager::addBook(book::Book const&)'
collect2: ld returned 1 exit status
[Finished in 0.2s with exit code 1]
[shell_cmd: g++ "/home/sakthi/MyProj/LibarayManager/src/LibraryManagerTest.cpp" -o "/home/sakthi/MyProj/LibarayManager/src/LibraryManagerTest"]
[dir: /home/sakthi/MyProj/LibarayManager/src]
[path: /usr/local/bin:/usr/bin:/bin:/usr/games]

我在這里想念什么嗎? 無法弄清楚

您沒有定義void addBook(const book::Book& book); LibraryManager.cpp文件中的函數

而且您在Book.h和Book.cpp中具有不同的命名空間...因此,您已經在.h文件中聲明了另一本書,並在.cpp文件中定義了新本書...我想是因為您忘記添加#include "Book.h"到Book.cpp。

更新資料

我最終重新編寫了您的代碼...大量的錯誤。 不使用const,加擾的代碼。 在編寫內容時進行更多編譯:

書本

#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>

#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;
    }

LibraryManager.h

#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 

LibraryManager.cpp

#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;
    }
}

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;*/

    }
}

}

仍然我沒有測試...

如果頭文件,則聲明

void addBook(const book::Book& book);

但是在.cpp中,您將其稱為addBooks

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

因此,您尚未實現addBook 這就是為什么在調用addBook在main中出現鏈接器錯誤的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM