Implementing a messaging system using one queue and 3 classe

Hi. I have an assignment that asks me to make a messaging system. These are the instructions.
Messages are received in the order they are sent
The classes involved are:
-Message
-MessageSender
-MessageReceiver

A Message object has a sender, recipient, content string (make an array of chars!) and a date.
A Message is placed in a queue by a MessageSender object.
A Message is removed from a queue by a MessageReceiver object, which can also displays the content of the queue.
Your queue class can receive any types of objects, including Message Objects.

Now, I dont really need you guys to just give me a complete program and that's it. I would apreciate it if you would give me some tips on how to aproach this. Like, how do I have all those 3 classes know of eachother? Any other tips are welcome.
 
Your spec is a little vague.
So is your question, actually. :) (how will they "know" each other?)

How are you ultimately going to drive these objects?
Do you have a main?
If not, what would it look like?
I do have a main. I have to send a Message object in the queue through a MessageSender object, but when I try to create the function in MessageSender class that sends Messages in the queue, I get an error when trying to make the function receive a Message object as parameter. Example: send(Message m){}. I tried to put each class in its own header file and then import everything that was needed in each header, but this created an "include nested too deeply" issue which I cannot solve. Is there any way to do this without relying on separate header files and lots of includes?
Did you use include guards on the header files? That ensures that they are not included more than once.
1
2
3
4
5
6
7
8
9
10
// message.h
#ifndef MESSAGE_CLASS
#define MESSAGE_CLASS 1
class Message {
    //sender     <-- how are you representing these ?
    //recipient  <--
    //content
    //date
};
#endif 

And make sure you include only what you need in the header files and never say "using namespace std" in an include file. Then include the necessary headers in the source files.
Ok so I put everything in header files and used include guards. I got only few errors left but they are driving me insane. My Queue class, which worked perfectly fine before splitting everything into headers, now gives an error saying 'Queue<T>::queueArray' has incomplete type. How is this possible? I am using a template class with typename T. Here is the code:
1
2
3
4
5
6
7
8
9
#include <stdio.h>
#ifndef QUEUE_H
#define QUEUE_H
#define NMAX 100

template<typename T> class Queue {
    private:
        T queueArray[NMAX];
        int head, tail;
And here is the Message class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string.h>
#include <stdio.h>

#ifndef MESS_H
#define MESS_H

#include "MessageSender.h"
#include "MessageReceiver.h"



class MessageSender;
class MessageReceiver;

class Message{

    private:
    MessageSender sender;
    MessageReceiver recipient;
    char content[100];
    string date;

    public : Message(MessageSender a, MessageReceiver b, char c[100], string d){
    sender = a;
    recipient = b;
    strcpy(content, c);
    date = d;
    }
    Message(){}

};
#endif 
If there's more to that error message, post it. And you'll probably have to post all your code (or put a link to a zip file that you've uploaded somewhere).

Also, be sure in the headers to put everything inside the include guards, including other includes. So put the include <stdio.h> inside the include guards. However, only put it in the header file if it is really needed IN THE HEADER FILE. If it's needed in the code file, put it there, not in the header file. (Actually, why are you including stdio.h in a C++ program anyway?)
Uploaded the whole project on Drive. Here's the link and thank you so much for helping!
https://drive.google.com/file/d/1BQHEXCVFsI3_eht7vqtHWLAxEiHmNTAT/view?usp=sharing
Also, I only needed stdio.h in my Queue class, for the fprintf function. I removed it from every other class.
So the basic problem is that since you are relying on a forward reference of Message to introduce that identifier into MessageSender.h you can only declare a pointer or reference to it. You should change all the Messages to pointers which means you'll allocate them with new and remember to delete them too.

BTW, instead of fprintf(stderr,"message"), use std::cerr << "message" (and get rid of that stdio.h! :)

Also, I can't say I understand why MessageSender and MessageReceiver are inheriting from Queue.
Last edited on
Got it. But I still don't understand why I'm getting that error in Queue.h. What is the problem there?
There is no problem in Queue.h. But since it's a template, it gets instantiated with the given type T when it is called with a given type. You were trying to instantiate it with an incomplete type (Message). If you had tried to instantiate it with int it would have worked just fine.

And like I mentioned, I don't think any of your classes should be derived from Queue (or any other class). I think you need one message queue that they both use.
I have to include the Queue header in both MessageSender and MessageReceiver because they will both have methods which use enqueue, dequeue and other functions of Queue.
You aren't just including Queue.h in MessageSender, you are deriving MessageSender from Queue.
 
class MessageSender : public Queue<Message*> {    //// I changed Message to a pointer 

I'm not sure you should be doing that, but I haven't entirely grokked the higher-level concept of your program so I could be wrong. It seems to me that, although it needs to deal with a message queue, it doesn't need to be derived from one or even to contain one as a member variable. Although it may need a pointer or reference to the message queue. Or maybe not. It depends on exactly how your real main will use the objects.
Last edited on
Yes, you're right. It doesn't need to be derived from one. I'll remove that. Also my main will be something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Queue<Message> msgQueue;
Message msg("Michael", "Andrew", "How u doin'?", 02.04.2018);

MessageSender.send(msg);//messageSender sends message objects in the queue using a
//send() function that enqueues the object in msgQueue;

MessageReceiver.retrieve(msg);//messageReceiver takes the message out of the queue
//using a retrieve function that deques the message.
MessageReceiver.display();//he can also display the whole queue of messages.

//Not exactly sure how I will implement the send function so that it can take messages
//and enqueue them in a queue that I will declare in main. Maybe I shouldn't declare my queue in main 
//but in MessageSender and Receiver classes? No idea yet
Last edited on
Topic archived. No new replies allowed.