2014-01-10 69 views
0

嗨我面臨着「未定義的引用librarymanager :: getBook(std :: basic_string,std :: allocator>)「儘管花費了大量的時間來弄清楚。我的代碼如下。未定義的引用`librarymanager :: getBook(std :: basic_string <char,std :: char_traits <char>,std :: allocator <char>>)

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

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 

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] 

我失去了一些東西在這裏?無法弄清楚

+1

我不是舒爾,但我認爲,在LibraryManager.cpp你應該定義書::圖書與librarymanager :: getBook(字符串名稱) {...出於命名空間 –

+0

'getBook'返回對局部變量的引用。這會導致你的問題! – Sean

+1

,應該把它的參數作爲const的參考:const std :: string& –

回答

1

如果頭文件聲明

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

但在.cpp你叫它addBooks

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

所以,你沒有實現addBook。這就是爲什麼你在main中得到一個鏈接器錯誤,因爲你叫addBook

+0

即使在糾正該錯誤後,我也會得到同樣的錯誤。謝謝。 – sakthisundar

2

你沒有定義在LibraryManager.cpp void addBook(const book::Book& book);功能文件

而且你必須在Book.h和Book.cpp不同的命名空間......所以,你已經宣佈另一本書中的.h文件中定義新的.cpp文件...我想因爲你忘記了將#include "Book.h"添加到Book.cpp中。

更新

我結束了rewritting你的代碼......巨大的失誤量。不使用const,亂碼。編譯更多的時候你是什麼書面方式:

Book.h

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

    } 
} 

} 

不過我沒有測試它...

+0

即使糾正了該錯誤,我仍然收到同樣的錯誤。謝謝。 – sakthisundar

相關問題