C++;对象的动态数组,函数中引用了错误LNK2019 我试图用C++编写一个图书馆系统程序。该课程包括图书、学生和图书馆系统课程。Library system类将具有动态book和student数组,例如,ı可以将book或student添加到系统中。在库系统的头文件中,我添加了 private: int numberOfBooks; int numberOfStudents; Book* books; Student* students;
这里没问题,但在它的cpp文件中C++;对象的动态数组,函数中引用了错误LNK2019 我试图用C++编写一个图书馆系统程序。该课程包括图书、学生和图书馆系统课程。Library system类将具有动态book和student数组,例如,ı可以将book或student添加到系统中。在库系统的头文件中,我添加了 private: int numberOfBooks; int numberOfStudents; Book* books; Student* students;,c++,arrays,pointers,lnk2019,C++,Arrays,Pointers,Lnk2019,这里没问题,但在它的cpp文件中 LibrarySystem::LibrarySystem() { numberOfBooks = 0; numberOfStudents = 0; books = new Book[ numberOfBooks ]; students = new Student[ numberOfStudents ]; } 它给出了一个类似于 Error 1 error LNK2019: unresolved external symb
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
\ifndef BOOK\u H
#定义书
#包括
#包括
使用名称空间std;
课堂用书
{
公众:
书籍(常量int anId、常量int aYear、常量字符串aTitle、常量字符串A作者、常量int aStudentId、常量字符串aStudentName);
书籍(常数、常数、常数、常数字符串、常数字符串作者);
书();
无效的收尾(const int anId);
int getBookId();
无效设置年(每年常量);
int getYear();
无效设置标题(常量字符串标题);
字符串getTitle();
void setAuthors(常量字符串aAuthors);
字符串getAuthors();
void setStudent(常量int aStudentId,常量字符串aStudentName);
int getStudentId();
字符串getStudentName();
bool被保留();
无效保留();
字符串printBook();
私人:
国际图书编号;
国际年;
字符串标题;
字符串作者;
字符串studentName;
国际学生;
布尔保留地;
};
#恩迪夫
#ifndef学生会
#定义学生
#包括
#包括
#包括“Book.h”
使用名称空间std;
班级学生
{
公众:
学生(整数,字符串,书本*,整数);
学生(整数,字符串);
学生();
~Student();
void setName(常量字符串aName);
字符串getName();
void setId(const int anId);
intgetid();
无效收件簿(书籍*aBooks,在一本书中的常数);
Book*getBooks();
布尔·哈萨尼书();
bool hasBook(bookabook);
字符串printStudent();
int getNumberOfBooks();
私人:
int-id;
国际数字图书;
字符串名;
书籍*书籍;
布尔伊夫本;
};
#恩迪夫
该错误意味着您没有声明Book
和Student
的参数构造函数,但从未提供实现
您需要在cpp文件中为这些构造函数编写代码,以修复链接错误,或在头中提供内联实现,或删除无参数构造函数的声明,并通过为其所有参数提供默认值使其他构造函数之一成为默认构造函数
注意:
Book
的构造函数在其他参数中包含学生ID看起来非常可疑,因为Book
对象会意识到与student
对象的关联。理想情况下,这种关联应该保持在Book
和Student
类之外。它抱怨您没有实现Book:Book
(或者您忘记了编译输出并将其链接到您的解决方案中)Book*books;学生*学生代码>我开始怀疑std::vector类是否有传染性疾病或其他东西,并且没有人想使用它。