I’m trying to create a MessageQueue
class that derives from the AbstractMessageQueue
class.
inside MessageQueue
I created a Node
class, the problem is that by doing a push
from main, the program remains in a “loop” and never ends… even worse if I call a push
the second time, it starts spamming in loop the contents of the message.
I’m writing directly inside the .h files to make it faster…
Here’s the code…
MessageQueue.h
#ifndef MESSAGE_QUEUE_H
#define MESSAGE_QUEUE_H
#include "AbstractMessageQueue.h"
#include <iostream>
using std::cout;
using std::endl;
class MessageQueue : public AbstractMessageQueue{
public:
MessageQueue() : head(nullptr), tail(nullptr) {}
~MessageQueue() override {
clear();
}
void push(const Message& msg) override {
Node* n = new Node(msg, nullptr);
cout << n->val.content() << endl;//checking if it's copied
if (head == nullptr) {
head = tail = n;
}
else {
tail->next = n;
tail = n;
}
} // aggiunge un messaggio in coda
Message pop() override{ // estrae un messaggio dalla coda
Node* p = head;
bool loop = true;
if(head != nullptr){
while (loop){
if (p->next != nullptr){
p = p->next;
cout << p->val.content() << endl;
}
else
loop = false;
}
delete p->next;
p->next = nullptr;
tail = p; // aggiornata la coda
}
Message msg("a","b","c");
return msg;
}
bool empty() const override { // true se la coda è vuota
if (head == nullptr)
return true;
return false;
}
void clear() override {
while (!empty())
pop();
} // pulisce la coda rimuovendo tutti i messaggi
Message getMessage(int i) const{
if (i < 0) {
throw std::out_of_range("L'indice non può esssere negativo");
}
Node* p = head;
for (int j = 0; j < i; ++j) {
if (p == nullptr) {
throw std::out_of_range("Indice fuori dal range");
}
p = p->next;
}
if (p == nullptr) {
throw std::out_of_range("Indice fuori dal range");
}
return p->val;
}
private:
class Node {
public:
Message val;
Node* next;
Node(const Message& val, Node* next) : val{ val }, next{ next } {}
};
Node* head = nullptr;
Node* tail = nullptr;
};
#endif
Message.h
#ifndef MESSAGE_H
#define MESSAGE_H
#include <string>
#include <ostream>
using std::string;
using std::ostream;
class Message{
public:
Message(const string& src, const string& dst, const string& content): m_src { src }, m_dst{ dst }, m_content{ content } {};
Message(const Message& msg) {
this->m_src = msg.src();
this->m_dst = msg.dst();
this->m_content = msg.content();
}
Message(void) {};
string src() const { return m_src; }
string dst() const { return m_dst; }
string content() const { return m_content; }
private:
string m_src;
string m_dst;
string m_content;
};
ostream& operator<<(ostream& os, const Message& msg) {
return os << msg.src() << "->" << msg.dst() << ": " << msg.content() << std::endl;
}
#endif
AbstractMessageQueue:
#ifndef ABSTRACT_MESSAGE_QUEUE_H
#define ABSTRACT_MESSAGE_QUEUE_H
#include "Message.h"
class AbstractMessageQueue {
public:
virtual void push(const Message& msg) = 0; // aggiunge un messaggio in coda
virtual Message pop() = 0; // estrae un messaggio dalla coda
virtual bool empty() const = 0; // true se la coda è vuota
virtual void clear() = 0;
virtual ~AbstractMessageQueue() {};
};
#endif
Main:
#include <iostream>
#include <string>
#include "Message.h"
#include "MessageQueue.h"
using namespace std;
int main() {
MessageQueue fucking;
fucking.push(Message{"alice","bob","hello"});
try {
fucking.getMessage(1);
}catch(const std::out_of_range& e) {
cout << "Exception: " << e.what() << endl;
}
//fucking.push(m2);
//fucking.push(m3);
return 0;
}
New contributor
Michele Fabiani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3