gpt4 book ai didi

c++ - 从头文件调用函数

转载 作者:行者123 更新时间:2023-11-28 02:30:25 24 4
gpt4 key购买 nike

作业
我试图从我的三个头文件中的每一个调用我的主要函数中的两个函数。

#ifndef UNSORTEDPQ_H
#define UNSORTEDPQ_H

#include <exception>
#include <cstddef>
#include <cmath>
using namespace std;

template <class Type>
class UnsortedPQ
{
private:
struct DLLNode {
Type data;
DLLNode *prev;
DLLNode *next;
};

DLLNode *head, *tail, *node;

public:
void insertUPQ ( Type data ){
DLLNode *newnode = new DLLNode;
newnode -> node = data;
newnode -> prev = NULL;
if (isEmpty()){
newnode -> next = NULL;
head = tail = newnode;
}
else {
newnode -> next = NULL;
newnode -> prev = node -> prev -> next;
tail = newnode;
}
}

// removes and returns the minimum value in the queue
// throws an exception if the queue is empty
Type removeUPQ ( void ) throw(exception){
if (isEmpty()) {
exception("Can't dequeue empty Priority Queue");
}

Type min = head -> data;

// special case: queue has one element
if (size()==1){
while (head != NULL) {
DLLNode *next = head -> next;
delete head;
head = next;
}
return min;
}

// determine minimum element
for (node = head; node != NULL; node = node -> next){
if (node -> data < min) {
min = node -> data;
}
}
// special case: queue has two elements
if (size()==2){
if (head -> data== min){
delete head;
tail -> previous = NULL;
head = tail;
}
else {
delete tail;
head -> next = NULL;
tail = head;
}
return min;
}

// remove element and update next and previous for elements around it
for (node = head; node != NULL; node = node -> next){
if (node -> data == min){
// special case if min elem is head
if (node == head){
DLLNode*oldHead=head;
head = head -> next;
head -> prev = NULL;
delete oldHead;
return min;
}
// special case if min elem is tail
if(node == tail){
DLLNode*oldTail=tail;
tail = tail-> prev;
tail -> next = NULL;
delete oldTail;
return min;
}
// for cases where min elem is not head or tail
DLLNode *prevNode = node -> prev;
DLLNode *nextNode = node -> next;
prevNode -> next = node -> next;
nextNode -> prev = node -> prev;
delete node;
return min;
}
}

#ifndef SORTEDPQ_H
#define SORTEDPQ_H

#include <exception>
#include <cstddef>
#include <cmath>
using namespace std;

template <class Type>
class SortedPQ
{
private:
struct DLLNode {
Type data;
DLLNode *prev;
DLLNode *next;
};

DLLNode *head, *tail, *node;


public:

// inserts a piece of data into the priority queue
void insertSPQ ( Type data ){
DLLNode curr = head;
while(curr != tail && node -> data <= curr -> data){
curr = curr -> next;
}
DLLNode tempCurr = curr;
node -> next = tempCurr;
node -> prev = tempCurr -> prev;
tempCurr -> prev -> next = node;
tempCurr -> prev = node;
}
// removes and returns the minimum value in the queue
// throws an exception if the queue is empty
Type removeSPQ ( void ) throw(exception){
if (isEmpty()) {
exception("Can't dequeue an empty Priority Queue");
}
Type min = head -> node;
if (node == head){
DLLNode*oldHead=head;
head = head -> next;
head -> prev = NULL;
delete oldHead;
return min;
}

#ifndef HEAPPQ_H
#define HEAPPQ_H

#include <exception>
#include <cstddef>
#include <cmath>
using namespace std;

template <class Type>
class HeapPQ
{
private:
Type *data;
Type heapSize;
Type arraySize;
Type floor(Type arg);
Type getLChildIndex(Type nodeIndex){
return 2 * nodeIndex + 1;
}

Type getRightChildIndex(Type nodeIndex){
return 2 * nodeIndex + 2;
}

Type getParentIndex(Type nodeIndex){
return floor((nodeIndex - 1) / 2);
}

public:
void HeapPropertyInsert(Type nodeIndex){
Type parentIndex, tmp;
if(nodeIndex != 0){
parentIndex = getParentIndex(nodeIndex);
if(data[parentIndex] > data[nodeIndex]){
tmp = data[parentIndex];
data[parentIndex] = data[nodeIndex];
data[nodeIndex] = tmp;
HeapPropertyInsert(parentIndex);
}
}
}

// inserts a piece of data into the priority queue
void insertHPQ ( Type data ) throw(exception){
if (heapSize == arraySize){
exception ("Cannot insert, heap is full");
}
else{
heapSize++;
data[heapSize - 1] = data;
HeapPropertyInsert(heapSize - 1);
}
}

void HeapPropertyRemove(Type nodeIndex){
Type leftChildIndex, rightChildIndex, minIndex, tmp;
leftChildIndex = getLChildIndex(nodeIndex);
rightChildIndex = getRightChildIndex(nodeIndex);
if(rightChildIndex >= heapSize){
if(leftChildIndex >= heapSize)
return;
else
minIndex = leftChildIndex;
}
else{
if (data[leftChildIndex] < data[rightChildIndex])
minIndex = leftChildIndex;
else
minIndex = rightChildIndex;
}
if(data[nodeIndex] > data[minIndex]){
tmp = data[minIndex];
data[minIndex] = data[nodeIndex];
data[nodeIndex] = tmp;
HeapPropertyRemove(minIndex);
}
}

// removes and returns the minimum value in the queue
// throws an exception if the queue is empty
Type removeHPQ ( void ) throw(exception){
if(isEmpty())
exception("Cannot remove, heap is empty");
else{
data[0] = data[heapSize - 1];
heapSize--;
if(heapSize > 0)
HeapPropertyRemove(0);
}
}

我的 main.cpp:

#include "UnsortedPQ.h"
#include "SortedPQ.h"
#include "HeapPQ.h"
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>

using namespace std;
int main(){

int choice;
int elem;
int array_size = 1024;
char * array = new char[array_size];
int position = 0;

ifstream fin("numbers.txt");
if(fin.is_open()){
cout << "File opened successfully." << endl;
while(!fin.eof() && position < array_size){
fin.get(array[position]);
position++;
}
array[0] = choice;
switch (choice){
case '0': //UnsortedPQ
for(int i = 2; array[i]; i++ ){
insertUPQ(array[i]); <----- error
removeUPQ(array[i]); <----- error
}
break;
case '1': //SortedPQ
for(int i = 2; array[i]; i++ ){
insertSPQ(array[i]); <----- error
removeSPQ(array[i]); <----- error
}
break;
case '2':// HeapPQ
for(int i = 2; array[i]; i++ ){
insertHPQ(array[i]); <----- error
removeHPQ(array[i]); <----- error
}
break;
}
}
else{
cout << "File could not be opened." << endl;
}
return 0;
}

我不断收到编译器错误,指出这些函数“未在此范围内声明”。我想,既然包含了头文件并且函数是公开的,那么我可以在 main.cpp 的任何地方随意调用它们。同样,这是家庭作业,所以我不是在寻求快速解决方案,但非常感谢您提供一些建议或指向正确方向的观点。

最佳答案

您尝试调用的所有函数都是属于特定方法。为了使用这些方法,您必须创建一个特定对象:

char * array = new char[array_size];

UnsortedPQ<char> Object;

Object.insertUPQ(array[i]);

有一个教程链接解释了 C++ 中的类到底是什么,如何创建和使用它们:

http://www.cplusplus.com/doc/tutorial/classes/

关于c++ - 从头文件调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29153536/

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