Ошибка:
Код: Выделить всё
make
g++ -O2 src/test.cpp -o build/program.exe
In file included from src/test.cpp:7:
src/priority-queue.hpp: In member function ‘void DSLib::PriorityQueue::EnQueue(T)’:
src/priority-queue.hpp:10:13: error: ‘Node’ was not declared in this scope
10 | Node* current = this->top;
| ^~~~
src/priority-queue.hpp:10:19: error: ‘current’ was not declared in this scope
10 | Node* current = this->top;
| ^~~~~~~
src/priority-queue.hpp:14:19: error: ‘n’ was not declared in this scope
14 | Node* n = new Node(Value, current, (current != nullptr) ? current->prev : nullptr);
| ^
src/priority-queue.hpp:14:27: error: expected type-specifier before ‘Node’
14 | Node* n = new Node(Value, current, (current != nullptr) ? current->prev : nullptr);
| ^~~~
src/priority-queue.hpp:14:26: error: expected ‘;’ before ‘Node’
14 | Node* n = new Node(Value, current, (current != nullptr) ? current->prev : nullptr);
| ^~~~~
| ;
make: *** [Makefile:9: build/program.exe] Error 1
Код: Выделить всё
#ifndef PRIORITY_QUEUE
#define PRIORITY_QUEUE
#include "queue.hpp"
namespace DSLib {
template
class PriorityQueue : public Queue {
public:
void EnQueue(T Value) {
Node* current = this->top;
while (current != nullptr && Compare(Value, current->value)) {
current = current->next;
}
Node* n = new Node(Value, current, (current != nullptr) ? current->prev : nullptr);
if (current != nullptr) {
if (current->prev != nullptr) {
current->prev->next = n;
} else {
this->top = n;
}
current->prev = n;
} else {
this->bottom = n;
}
this->size++;
}
};
}
#endif
Код: Выделить всё
#ifndef QUEUE_HPP
#define QUEUE_HPP
#include
namespace DSLib {
template
class Queue {
protected:
struct Node {
public:
T value;
Node* next;
Node* prev;
Node(T Value, Node* Next = nullptr, Node* Prev = nullptr) : value(Value), next(Next), prev(Prev) {}
};
int size;
Node* top = nullptr;
Node* bottom = nullptr;
public:
Queue() : size(0), top(nullptr), bottom(nullptr) {}
~Queue() {
while (top != nullptr) {
DeQueue();
}
}
bool IsEmpty() {
return size next = newNode;
bottom = newNode;
if (top == nullptr)
top = newNode;
}
T DeQueue() {
if (IsEmpty()) {
throw std::underflow_error("DeQueue Error: Queue Empty");
}
size--;
T dequeuedValue = top->value;
Node* oldTop = top;
top = top->next;
if (top != nullptr)
top->prev = nullptr;
else
bottom = nullptr;
delete oldTop;
return dequeuedValue;
}
T Peek() {
if (IsEmpty()) {
throw std::underflow_error("Peek Error: Queue Empty");
}
return top->value;
}
void Sort(bool (*comparator)(const T&, const T&) = [](const T& a, const T& b) { return a < b; }) {
Queue sortedQueue;
Queue tempQueue;
while (!IsEmpty()) {
T minVal = Peek();
while (!IsEmpty()) {
T val = DeQueue();
if (comparator(val, minVal)) {
minVal = val;
}
tempQueue.EnQueue(val);
}
bool minAdded = false;
while (!tempQueue.IsEmpty()) {
T val = tempQueue.DeQueue();
if (val == minVal && !minAdded) {
sortedQueue.EnQueue(val);
minAdded = true;
} else {
EnQueue(val);
}
}
}
while (!sortedQueue.IsEmpty()) {
EnQueue(sortedQueue.DeQueue());
}
}
class Iterator {
private:
Node* current;
public:
Iterator(Node* Start) : current(Start) {}
bool operator!=(const Iterator& Other) const {
return current != Other.current;
}
Iterator& operator++() {
if (current) current = current->next;
return *this;
}
T& operator*() const {
return current->value;
}
};
Iterator begin() const {
return Iterator(top);
}
Iterator end() const {
return Iterator(nullptr);
}
};
}
#endif
Неважно, я это исправил. мне просто нужно было сделать typename Queue::Node.
Подробнее здесь: https://stackoverflow.com/questions/791 ... in-scope-c
Мобильная версия