C ++动态对象数组,函数中引用了错误LNK2019

时间:2014-03-17 17:38:42

标签: c++ arrays pointers lnk2019

我正在尝试用c ++编写一个库系统程序。在该计划中,有书籍,学生和图书馆系统课程。图书馆系统类将具有动态书籍和学生数组,以便例如ı可以将书籍或学生添加到系统中。在库系统的头文件中,我添加了

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

这里没有问题,但在cpp文件中,

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

它会出现类似

的错误
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

这是什么问题?我只是想创建动态数组。还有其他课程:

#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

1 个答案:

答案 0 :(得分:1)

错误意味着您已声明BookStudent的无参数构造函数,但您从未提供过实现。

您需要在cpp文件中为这些构造函数编写代码以修复链接错误,或者在标头中提供内联实现,或者删除no-arg构造函数的声明,并使其他构造函数之一成为默认值通过为其所有参数提供默认值。

注意:拥有Book的构造函数,其中包含其他参数,学生ID看起来非常可疑,因为Book对象会意识到与Student对象的关联。理想情况下,此关联应保持在BookStudent类之外。