gpt4 book ai didi

c++ - 从我的 driver.cpp 调用派生类

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

如果能提供一些帮助,我将不胜感激。在我的计算机科学课上,我们被要求做一些我以前从未做过的事情,而且我对我的问题的理解不够透彻,甚至不知道用什么术语来谷歌。

我们有三个文件。 lists.h、lists.cpp 和 driver.cpp

lists.h - 这是教授提供的,我不能更改。它是我将从中派生新类的基类...

lists.cpp - 在这里,我在 DLList 类中实现双向链表。我以前做过dll,但不是这样。所以你会在这里看到很多代码,尽管我无法测试任何代码。如果我的函数在这里有误,请不要担心,我只是想在...中创建一个双向链表

driver.cpp - 这是我将用来测试我在 DLList 类中的函数的文件。

所以无论如何,我编译并得到....

g++ -c -g -Wall -std=c++11 driver.cpp
driver.cpp: In function ‘int main()’:
driver.cpp:12:5: error: ‘DLList’ was not declared in this scope
DLList<int> mylist;
^

我理解错误。我以前在 main 没有看到函数/类/等时看到过这个。在更简单的代码中,这是因为它在 main 之后。但是在这里,我只是不知道如何解决它。显然driver.cpp看不到我在lists.cpp中的代码。

这是我的代码——

不可改变的列表.h

template<typename E> class List {
private:
void operator =(const List&) {} // Protect assignment
List(const List&) {} // Protect copy constructor
public:
List() {} // Default constructor
virtual ~List() {} // Base destructor

// Clear contents from the list, freeing memory
virtual void clear() = 0;

// Insert an element at the beginning of the list.
virtual void prepend(const E& item) = 0;

// Append an element at the end of the list.
virtual void append(const E& item) = 0;

// Extra credit: Insert an element at the current location, if possible;
// return true if successful, false if there is no current element
virtual bool insert(const E& item) = 0;

// Extra credit: Remove and assign to item the current element, if possible;
// return true if successful, false if there is no current element
virtual bool remove(E& item) = 0;

// Set the current position to the first element of the list, if possible;
// return true if successful, false if list was empty
virtual bool moveToStart() = 0;

// Set the current position to the last element of the list, if possible;
// return true if successful, false if list was empty
virtual bool moveToEnd() = 0;

// Move the current position one step right, if possible;
// return true if successful, false if already at the end
virtual bool next() = 0;

// Move the current position one step left, if possible;
// return true if successful, false if already at the beginning
virtual bool prev() = 0;

// Return a pointer to the current element (or NULL if none)
virtual const E* getValue() const = 0;

// Return total number of active nodes
virtual int numActive() = 0;

// Return total number of free nodes
virtual int numFree() = 0;
};


// Factory function
template<typename E> List<E> *createList();

列表.cpp

#include "lists.h"
#include <cstddef>
#include <iostream>

using namespace std;

// Doubly linked list link node with freelist support
template <typename E> class Link {
private:
// required by Lab 4
static int free; // # of nodes free
static int active; // # of nodes in use
static Link<E> *freelist; // Reference to freelist head

E element; // Value for this node
Link *next; // Pointer to next node in list
Link *prev; // Pointer to previous node

public:
// Constructors
Link(const E& it, Link *prevp=NULL, Link *nextp=NULL) {
element = it;
prev = prevp;
next = nextp;
}

Link(Link *prevp =NULL, Link *nextp =NULL) {
prev = prevp;
next = nextp;
}

void * operator new(std::size_t) { // Overloaded new operator
active++; // add to active count

if (freelist == NULL) { // Create space
return ::new Link; // ::new means use the standard c++ new operator
}

Link<E> *temp = freelist; // Can take from freelist
freelist = freelist->next;
free--; // will only subtract if we take from freelist

return temp; // Return the link
}

// Overloaded delete operator
void operator delete(void* ptr) {
free++;
active--;
// eliminate the Link being deleted from the active list
Link *prev_tmp=((Link<E>*)ptr)->prev;
Link *next_tmp=((Link<E>*)ptr)->next;
prev_tmp->next=next_tmp;
next_tmp->prev=prev_tmp;

((Link<E>*)ptr)->next = freelist; // Attach deleted Link to the head of the freelist
freelist = (Link<E>*)ptr; // Now redefine the freelist to the new head
}

E get_data() const {
return element;
}

E set_data(E& it) {
element=it;
}

Link<E> *get_next() const {
return next;
}

Link<E> *get_ptrb() const {
return prev;
}

void set_next(Link<E> *new_next ) {
next = new_next;
}

void set_prev(Link<E> *new_prev) {
prev = new_prev;
}

int get_free() {
return free;
}

int get_active(){
return active;
}

};
// The freelist head pointer is actually created here
template <typename E>
Link<E> *Link<E>::freelist = NULL;




template <typename E> class DLList: public List<E> {
private:
Link<E> *head; // Pointer to list header
Link<E> *tail; // Pointer to last element
Link<E> *curr; // Access to current element

void operator =(const DLList&) {} // Protect assignment
DLList(const DLList&) {} // Protect copy constructor

public:
// Default constructor
DLList() {
head=NULL;
tail=NULL;
}

// Base destructor
~DLList() {
//delete
}

// Clear contents from the DLList, freeing memory
void clear() = 0;

// Insert an element at the beginning of the DLList.
void prepend(const E& item) {
Link<E> *newLink = new Link<E>(item);

if (head==NULL) {
head=newLink;
tail=newLink;
curr=newLink;
}
else {
newLink->set_next(head);
head=newLink;
curr=newLink;
}

}

// Append an element at the end of the DLList.
void append(const E& item) {
Link<E> *newLink = new Link<E>(item);

if (head==NULL) {
head=newLink;
tail=newLink;
curr=newLink;
}
else {
tail->set_next(newLink);
tail=newLink;
curr=newLink;
}

}

void print_list() {
if (head==NULL) {
cout << "EMPTY LIST" << endl;
}
else {
Link<E> *temp_ptr = head;
int index=0;
while(temp_ptr) {
cout << "Link " << index++ << ": " << temp_ptr->get_data() << endl;
temp_ptr = temp_ptr->get_next();
}
}
}

// Extra credit: Insert an element at the current location, if possible;
// return true if successful, false if there is no current element
bool insert(const E& item) {
if (curr==NULL) {
return false;
}
else {
Link<E> *newLink = new Link<E>(item);
Link<E> *prev_ptr=curr->get_prev;
Link<E> *next_ptr=curr->get_next();

prev_ptr->set_next(curr);
curr->set_prev(prev_ptr);

next_ptr->set_prev(curr);
curr->set_next(next_ptr);

return true;
}
}

// Extra credit: Remove and assign to item the current element, if possible;
// return true if successful, false if there is no current element
bool remove(E& item) {
if (curr==NULL) {
return false;
}
else {
curr->set_data(item);
}
}

// Set the current position to the first element of the DLList, if possible;
// return true if successful, false if DLList was empty
bool moveToStart() {
if (head==NULL) {
return false;
}
else {
curr=head;
return true;
}
}

// Set the current position to the last element of the DLList, if possible;
// return true if successful, false if DLList was empty
bool moveToEnd() {
if (head==NULL) {
return false;
}
else {
curr=tail;
return true;
}
}

// Move the current position one step right, if possible;
// return true if successful, false if already at the end
bool next() {
if (curr==tail) {
return false;
}
else {
curr=curr->get_next();
return true;
}
}

// Move the current position one step left, if possible;
// return true if successful, false if already at the beginning
bool prev() {
if (curr==head) {
return false;
}
else {
curr=curr->get_prev();
return true;
}
}

// Return a pointer to the current element (or NULL if none)
const E* getValue() {
if (curr==NULL) {
return NULL;
}
else {
curr->get_data();
}
}


// ******** MIGHT HAVE TO GET THIS DATA FROM MULTIPLE SOURCES IF THE PTR IS NULL!!
// BUG !
// Return total number of active nodes
int numActive() {
return curr->get_active();
}

// ******** MIGHT HAVE TO GET THIS DATA FROM MULTIPLE SOURCES IF THE PTR IS NULL!!
// BUG !
// Return total number of free nodes
int numFree() {
return curr->get_free();
}
};

// Explicit instantiation
template List<int> *createList();

驱动.cpp

#include <iostream>
#include "lists.h"
using namespace std;

void uppercaseify(string& mystr) {
for (auto& c: mystr)
c = toupper(c);
}

int main() {
createList<int>();
DLList<int> mylist;
return 0;
}

最佳答案

我认为您不应该在 main() 中显式实例化您的 DLList 类。这是行不通的,因为您在“driver.cpp”中没有可用的声明。

您应该做的是在“list.cpp”中实现工厂函数以返回动态分配的 DLList 实例:

template<> List<int> *createList() { return new DLList<int>; }

所以调用createList<int>()然后在“driver.cpp”中创建一个 DLList 实例,您可以通过 List 的接口(interface)对其进行测试:

int main() {
// Create a DLList through factory function.
List<int>* list = createList<int>();

// Use the DLList through the List interface.
list->append( ... );

// Finished with using list, free the memory
delete list; list = nullptr;

// Not necessary - we have created the DLList through factory function!
// DLList<int> mylist;
return 0;
}

关于c++ - 从我的 driver.cpp 调用派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42321206/

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