- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如果能提供一些帮助,我将不胜感激。在我的计算机科学课上,我们被要求做一些我以前从未做过的事情,而且我对我的问题的理解不够透彻,甚至不知道用什么术语来谷歌。
我们有三个文件。 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/
我有一个无法理解的奇怪编译问题。 //I know, you should never derive from the STL Library template class SharedClass :
我是一个刚开始学习 Haskell 的菜鸟,所以如果我问愚蠢的问题,请耐心等待。 最近我在 SO 中遇到了演示如何导出函数和表达式的类型和实现的问题(诸如 How can I understand "
如何自动派生此 GADT 的 Read 实例: {-# LANGUAGE GADTs, StandaloneDeriving #-} data TypeDec a where TypeDecInt
我遇到了我想要的情况 Deal class要注意它DealDetail type反之亦然,我想 DealDetail注意Deal type .将来我想有很多 Deal 的后代和 DealDetails
我是 C# 新手,所以请多多包涵。 好的,所以我在不同的程序集中有两个类需要相互引用: namespace AssemblyA { class A { private B MyB {
简而言之,我已经实现了一个派生自 SynchronizationContext 的类,以便 GUI 应用程序可以轻松地使用在 GUI 线程以外的线程上引发的事件。我非常感谢对我的实现的评论。具体来说,
我正在设计一个小型系统,想知道如何为派生类分配内存的细微差别。 如果我有两个类(class) class foo { public: int a; Foo(): a(0) {}; }; class
我正在尝试编写一个派生 PartialEq 的枚举,其中包含一个手动执行此操作的特征对象。我使用了解决方案 here为了强制 Trait 的实现者编写相等方法。这无法编译: trait Trait {
以下代码可以编译(特别是 MyError 被识别为具有调试特性): use std::str; use std::fmt; #[derive(Debug)] enum MyError where F:
是否有一种简单的方法来注释结构中的字段,以便在派生 PartialEq 特征时忽略它们?例如: #[derive(PartialEq,Eq)] pub struct UndirectedGraph {
我正在编写代码来处理“Foo”类型的对象。 foo 是一种容器,为了提供对其元素的高效和抽象访问,它提供了 Element 类型的嵌套类。 Element 包装对象在容器中的位置。 现在,“Foo”可
假设如下: class child : public parent { public: fun1(parent * obj); //somewhere on the child class
我有几个模板类 template class Transition { public: virtual Cost getCost() = 0; }; template class St
我正在尝试使用自定义 QSortFilterProxyModel . 这是我的标题: #include class QSortFilterProxyModel_NumbersLast : publi
我正在使用 C# 和 mvc3。我在解决方案中添加了一个项目。我想创建一个新 Controller 并让它从我添加的项目中的 Controller 派生。我该怎么做? 最佳答案 在 Visual St
我在 python 中有一个对象,它派生自 QtGui.QGraphicsPixmapItem,具有一些基本属性和方法。在对此对象的引用上调用 deepcopy 后,当我尝试使用该副本时收到一条错误消
由于只能给FixedDocument添加页面,所以我写了一个派生类: public class CustomFixedDocument : FixedDocument { public voi
我在自定义 QMainWindow 时遇到了很大的问题,因为我不知道如何实现以下内容: 在 QMainWindow 文档中,QMainWindow 有一些用于工具栏、停靠小部件、状态栏和其他的特殊区域
我想感受一下QT,决定写一个小的十六进制编辑器。为此,我需要一个允许滚动的小部件。经过一番研究,我发现 QTextEdit 为此目的派生自 QAbstractScrollArea。在阅读 QAbstr
我正在寻找一种可以从已经发生的洗牌过程中派生出 key 的算法。 假设我们有被打乱的字符串“Hello”: "hello" -> "loelh" 现在我想从中导出一个 key k,我可以用它来撤销洗牌
我是一名优秀的程序员,十分优秀!