So, I am trying to create a custom class, but can't figure out how to get it to work correctly. Here is what I have come up with:
message.h:
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fstream>
#include <string>
using namespace std;
class Message {
public:
Message(string, string, int);
~Message();
string getMessage();
string getSubject();
int getLength();
void setMessage(string);
void setLength(int);
void setSubject(string);
private:
string subject;
string message;
int length;
};
And the message.cc:
#include "message.h"
Message::Message(string Subject, string Message, int Length){
subject = Subject;
message = Message;
length = Length;
}
string
Message::getMessage(){
return message;
}
void
Message::setMessage(string Message){
message = Message;
}
string
Message::getSubject(){
return subject;
}
void
Message::setSubject(string Subject){
subject = Subject;
}
int
Message::getLength(){
return length;
}
void
Message::setLength(int Length){
length = Length;
}
Here is what I am trying to do:
map<string,vector<Message> > database;
string request = get_request(client);
//store the request in memory
vector<Message> messageList = database.at("user1");
messageList.push_back(new Message("subject", request, request.size()));
database["user1"] = messageList;
This code gives the following compile errors(because I am creating a new message):
//no known conversion for argument 1 from Message* to const Message&
But when I change the code to be:
//store the request in memory
vector<Message> messageList = database.at("user1");
Message message;
message.setMessage(request);
message.setSubject("subject");
message.setLength(request.length());
messageList.push_back(message);
database["user1"] = messageList;
it gives the following errors for Message message:
//No matching function for call to Message::Message()
//candidates are:
//Message::Message(str::string, str::string, int)
//Message::Message(const Message&)
//candidate expects 3 and 1 arguments, 0 provided
So, this leads me to believe that I am missing something in my message class (or header) that would allow for this type of instantiation, but I don't know how to do this or what I am missing. Any help would be really appreciated. I only have a very basic understanding of C++ since I have mostly programmed in Java, but no matter how much I try to look up the errors I get or code it in a different way I am not successful at compiling the code. Thanks again.