неопределенная ссылка на `librarymanager :: getBook (std :: basic_string & lt; char, std :: char_traits & lt; char & gt ;, std :: allocator & lt; char & gt; & gt;)

Привет, я столкнулся с проблемой «неопределенной ссылки на` 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;
}
}

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]

Я что-то здесь упускаю? Не в состоянии разобраться

0

Решение

Вы не определили void addBook(const book::Book& book); функция в файле LibraryManager.cpp

И у вас есть разные пространства имен в Book.h и Book.cpp … Итак, вы объявили другую книгу в файле .h и определили новую в файле .cpp … Я думаю, вы забыли добавить #include "Book.h" в Book.cpp.

Обновить

В итоге я переписал твой код … Огромное количество ошибок. Не используя 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;*/

}
}

}

Тем не менее я не проверял это …

2

Другие решения

Если вы заголовочный файл, вы объявляете

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

Но в .cpp вы назвали это addBooks:

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

Так вы не реализовали addBook, Вот почему вы получаете ошибку компоновщика в main, когда вы звоните addBook,

1

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector