未定义的引用`librarymanager :: getBook(std :: basic_string <char,std :: char_traits <char =“”>,std :: allocator <char>&gt;)</char> </char,>

时间:2014-01-10 10:40:43

标签: c++ string

您好我面临的问题是“对`librarymanager :: getBook(std :: basic_string,std :: allocator&gt;)的未定义引用”尽管花了很多时间搞清楚。我的代码如下。

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]

我在这里遗漏了什么吗?无法弄清楚

2 个答案:

答案 0 :(得分:2)

您没有在LibraryManager.cpp文件中定义void addBook(const book::Book& book);函数

你在Book.h和Book.cpp中有不同的命名空间...所以你已经在.h文件中声明了另一本书并在.cpp文件中定义了新的...我想因为你忘了添加{{1到Book.cpp。

<强>更新

我最终重写了你的代码......大量的错误。不使用const,乱码。在写东西时编译更多:

Book.h

#include "Book.h"

Book.cpp

#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

LibraryManager.h

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

#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 

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

我仍然没有测试它......

答案 1 :(得分:1)

如果您声明了头文件

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

但在.cpp中你称之为addBooks

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

所以你没有实现addBook。这就是你在main中遇到链接器错误的原因,因为你正在调用addBook

相关问题