gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:37:53 25 4
gpt4 key购买 nike

我正在尝试用 C++ 编写图书馆系统程序。在该计划中有书籍、学生和图书馆系统类(class)。图书馆系统类将有动态的书籍和学生数组,这样我就可以将书籍或学生添加到系统中。在库系统的头文件中,我添加了

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

最佳答案

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

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

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

关于C++动态对象数组,函数引用错误LNK2019,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22461390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com