Question

I am trying to write a library system program in c++. In the program there are book, student and library system classes. Library system class will have dynamic book and student array so that for instance ı can add book or student to the system. In the header file of the library system, I added

private:
    int     numberOfBooks;
    int     numberOfStudents;
    Book*       books;
    Student*    students;

No problem here but in the cpp file of it,

LibrarySystem::LibrarySystem()
{
    numberOfBooks = 0;
    numberOfStudents = 0;
    books = new Book[ numberOfBooks ];
    students = new Student[ numberOfStudents ];
}

it gives an error like

Error   1   error LNK2019: unresolved external symbol "public: __thiscall Book::Book(void)" (??0Book@@QAE@XZ) referenced in function "public: __thiscall LibrarySystem::LibrarySystem(void)" (??0LibrarySystem@@QAE@XZ) C:\Users\ŞEHZADE\Desktop\AKADEMİK\CS201\Homeworks\HW1\homework1\homework1\LibrarySystem.obj homework1
Error   2   error LNK2019: unresolved external symbol "public: __thiscall Student::Student(void)" (??0Student@@QAE@XZ) referenced in function "public: __thiscall LibrarySystem::LibrarySystem(void)" (??0LibrarySystem@@QAE@XZ)    C:\Users\ŞEHZADE\Desktop\AKADEMİK\CS201\Homeworks\HW1\homework1\homework1\LibrarySystem.obj homework1

What is the problem here? I just wanted to create dynamic arrays. Also other classes:

#ifndef BOOK_H
#define BOOK_H
#include <iostream>
#include <string>

using namespace std;

class Book
{
public:
    Book( const int anId, const int aYear, const string aTitle, const string aAuthors, const int aStudentId, const string aStudentName );
    Book( const int anId, const int aYear, const string aTitle, const string aAuthors );
    Book();
    void    setBookId( const int anId );
    int     getBookId();
    void    setYear( const int aYear );
    int     getYear();
    void    setTitle( const string aTitle );
    string  getTitle();
    void    setAuthors( const string aAuthors );
    string  getAuthors();
    void    setStudent( const int aStudentId, const string aStudentName );
    int     getStudentId();
    string  getStudentName();
    bool    isReserved();
    void    clrReservation();
    string  printBook();

private:
    int      bookId;
    int      year;
    string   title;
    string   authors;
    string   studentName;
    int      studentId;
    bool     reservation;
};

#endif

#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
#include "Book.h"

using namespace std;

class Student
{
public:
    Student( int, string, Book*, int );
    Student( int, string );
    Student( );
    ~Student();
    void    setName( const string aName );
    string  getName( );
    void    setId( const int anId );
    int     getId( );
    void    setBooks( Book *aBooks, const int aNumberOfBooks );
    Book*   getBooks( );
    bool    hasAnyBook( );
    bool    hasBook( Book aBook );
    string  printStudent( );
    int     getNumberOfBooks( );

private:
    int         id;
    int         numberOfBooks;
    string      name;
    Book*       books;
    bool        ifBook;
};
#endif
Was it helpful?

Solution

The error means that you have declared no-argument constructors of Book and Student, but you never provided an implementation.

You need to write code in the cpp files for these constructors to fix the link error, or provide an inline implementation in the header, or remove the declaration of the no-arg constructors, and make one of the other constructors the default by providing default values for all its arguments.

Note: having a constructor of the Book that takes among other parameters a student ID looks highly suspicious, because the Book object becomes aware of the association with a Student object. Ideally, this association should be kept outside Book and Student class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top