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.
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;
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?)
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.
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.
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