C++ 嗨,我很困惑为什么我会得到一个编译错误,主要是:“;Library.cc:(.text+;0x27):未定义对`PatronArray::PatronArray()'&引用;
这是我的Library.h文件,在库用来做所有肮脏的工作之前:在操作数组和其他方面,但现在我试图让库成为中间人,只调用与任何数组操作有关的调用。我的问题是我正在尝试一个用户数组实例,它将容纳图书馆中的所有用户C++ 嗨,我很困惑为什么我会得到一个编译错误,主要是:“;Library.cc:(.text+;0x27):未定义对`PatronArray::PatronArray()'&引用;,c++,oop,dynamic-allocation,C++,Oop,Dynamic Allocation,这是我的Library.h文件,在库用来做所有肮脏的工作之前:在操作数组和其他方面,但现在我试图让库成为中间人,只调用与任何数组操作有关的调用。我的问题是我正在尝试一个用户数组实例,它将容纳图书馆中的所有用户 #ifndef LIBRARY_H #define LIBRARY_H #include <string> #include "types.h" #include "Book.h" #include "Patron.h" #include "PatronArray.h" /
#ifndef LIBRARY_H
#define LIBRARY_H
#include <string>
#include "types.h"
#include "Book.h"
#include "Patron.h"
#include "PatronArray.h"
//class PatronArray
class Library
{
public:
Library();
~Library();
void init();
int addBook(Book*);
int addPatron(Patron*);
int remPatron(int);
int findBook(int, Book**, int*);
int findPatron(string, string, Patron**, int*);
int getMaxCollIndex();
int getMaxPatronsIndex();
Book* getBook(int);
Patron* getPatron(int);
private:
Book* collection[MAX_COLL_SIZE];
PatronArray* patrons;
int maxCollIndex;
int maxPatronsIndex;
};
#endif
#ifndef PATRONARRAY_H
#define PATRONARRAY_H
#include "Patron.h"
#include "Book.h"
#include "types.h"
//class Patron;
class PatronArray
{
public:
PatronArray();
~PatronArray();
int addPatron(Patron*);
int remPatron(int);
int findPatron(string, string, Patron**, int*);
int getMaxPatronsIndex();
Patron* getPatron(int);
private:
Patron* patrons[MAX_COLL_SIZE];
int maxPatronsIndex;
};
#endif
这是PatronArray.cc文件,我知道有更好的方法,通过模板制作,我现在还不了解,而且这种方法可以帮助我理解整个面向对象的风格
#include<iostream>
#include<string>
#include "PatronArray.h"
/*
* Default constructor: recheck this later
*/
PatronArray::PatronArray()
:maxPatronsIndex(0)
{
for (int i = 0; i < MAX_COLL_SIZE; ++i) {
patrons[i] = 0;
}
}
/*
* Destructor: recheck this later
*/
PatronArray::~PatronArray()
{
for (int i = 0; i < maxPatronsIndex; ++i)
delete patrons[i];
}
//get the maxindex
int PatronArray::getMaxPatronsIndex() { return maxPatronsIndex; }
/*
* Adds the given Patron to the given patrons Array
*/
int PatronArray::addPatron(Patron* patron)
{
if (maxPatronsIndex >= MAX_COLL_SIZE - 1) {
return C_NOK;
}
patrons[maxPatronsIndex++] = patron;
return C_OK;
}
/*
* Used for removing a patron in the patrons array
*/
int PatronArray::remPatron(int index)
{
if (index < 0 || index >= maxPatronsIndex)
return C_NOK;
delete patrons[index];
patrons[index] = 0;
return C_OK;
}
/*
* Searches for the patron; if found, sets the contents of the second
* parameter to that patron pointer, sets the contents of the third parameter to
* its index in the collection, and returns C_OK; if not found, sets the
* contents of the second parameter to zero, the theird to -1, and returns C_NOK
*/
int PatronArray::findPatron( string fn, string ln, Patron** patron, int* index)
{
for (int i = 0; i < maxPatronsIndex; ++i) {
if (patrons[i] == 0)
continue;
if (patrons[i]->getFname() == fn && patrons[i]->getLname() == ln) {
*patron = patrons[i];
*index = i;
return C_OK;
}
}
*patron = 0;
*index = -1;
return C_NOK;
}
Patron* PatronArray::getPatron(int index)
{
if (index < 0 || index >= maxPatronsIndex)
return 0;
return patrons[index];
}
#包括
#包括
#包括“PatronArray.h”
/*
*默认构造函数:请稍后重新检查
*/
用户阵列::用户阵列()
:最大用户索引(0)
{
对于(int i=0;i=MAX\u COLL\u SIZE-1){
返回C_NOK;
}
用户[maxPatronsIndex++]=用户;
返回C_OK;
}
/*
*用于删除用户阵列中的用户
*/
int用户数组::remconsor(int索引)
{
如果(索引<0 | |索引>=最大用户索引)
返回C_NOK;
删除用户[索引];
用户[索引]=0;
返回C_OK;
}
/*
*寻找赞助人;如果找到,则设置第二个
*参数,将第三个参数的内容设置为
*它在集合中的索引,并返回C_OK;如果未找到,则设置
*第二个参数的内容为零,theird为-1,并返回C_NOK
*/
int用户数组::findPatron(字符串fn,字符串ln,用户**用户,int*索引)
{
对于(int i=0;igetFname()==fn&&用户[i]->getLname()==ln){
*赞助人=赞助人[i];
*指数=i;
返回C_OK;
}
}
*用户=0;
*指数=-1;
返回C_NOK;
}
用户*用户数组::getPatron(int索引)
{
如果(索引<0 | |索引>=最大用户索引)
返回0;
返回用户[索引];
}
我忘了在makefile中将我的PatronArray.cc链接到Library.cc,谢谢你,杰克 PatronArray.cc
未与链接。检查您的构建配置。您是如何编译它的?您实际上声明并正确定义了PatronArray::PatronArray()
,这意味着PatronArray.cc
没有链接到Library.cc
,您能告诉我们编译时使用的命令吗?我使用的是makefileo该死的,我忘了在我的makefile中为PatronArray定义对象文件,现在就开始吧。
#include<iostream>
#include<string>
#include "PatronArray.h"
/*
* Default constructor: recheck this later
*/
PatronArray::PatronArray()
:maxPatronsIndex(0)
{
for (int i = 0; i < MAX_COLL_SIZE; ++i) {
patrons[i] = 0;
}
}
/*
* Destructor: recheck this later
*/
PatronArray::~PatronArray()
{
for (int i = 0; i < maxPatronsIndex; ++i)
delete patrons[i];
}
//get the maxindex
int PatronArray::getMaxPatronsIndex() { return maxPatronsIndex; }
/*
* Adds the given Patron to the given patrons Array
*/
int PatronArray::addPatron(Patron* patron)
{
if (maxPatronsIndex >= MAX_COLL_SIZE - 1) {
return C_NOK;
}
patrons[maxPatronsIndex++] = patron;
return C_OK;
}
/*
* Used for removing a patron in the patrons array
*/
int PatronArray::remPatron(int index)
{
if (index < 0 || index >= maxPatronsIndex)
return C_NOK;
delete patrons[index];
patrons[index] = 0;
return C_OK;
}
/*
* Searches for the patron; if found, sets the contents of the second
* parameter to that patron pointer, sets the contents of the third parameter to
* its index in the collection, and returns C_OK; if not found, sets the
* contents of the second parameter to zero, the theird to -1, and returns C_NOK
*/
int PatronArray::findPatron( string fn, string ln, Patron** patron, int* index)
{
for (int i = 0; i < maxPatronsIndex; ++i) {
if (patrons[i] == 0)
continue;
if (patrons[i]->getFname() == fn && patrons[i]->getLname() == ln) {
*patron = patrons[i];
*index = i;
return C_OK;
}
}
*patron = 0;
*index = -1;
return C_NOK;
}
Patron* PatronArray::getPatron(int index)
{
if (index < 0 || index >= maxPatronsIndex)
return 0;
return patrons[index];
}