我试图为一个基于控制台的简单电子邮件服务器定义两个向量;一个用于存储收件箱消息,另一个用于存储已发送的消息。每个消息对象(向量元素)都有3个字符串属性。并且感谢Galik的建议,我的更新代码更清晰,构造器制作完成,我的getter和setter也完成了。然而,尽管取得了所有这些进展,我仍然不确定如何(以及在何处)将3个属性分配到一个消息对象中以用作向量元素。我在新代码中该怎么做?:
标题文件:
//Message.h constructs the setup for email messages
#ifndef MESSAGE_H
#define MESSAGE_H
#include <string>
#include <vector>
//using namespace std;//should not use in header file.
//prototype functions
void viewInboxMessage();
class Message
{
private:
std::string address;
std::string subject;
std::string text;
std::vector<Message> inbox;
std::vector<Message> sent;
public:
Message()//default constuctor
{
//set all member variables to NULL at start of program:
vector<Message> inbox(0);
vector<Message> sent(0);
address = "";
subject = "";
text = "";
}
//contructor for message object
Message(string address, string subject, string text);
void setAddress(string addr);
string getAddress();
void setSubject(string subj);
string getSubject();
void setMessageText(string msgTxt);
string getMessageText();
};
#endif
.cpp函数文件(与具有main函数的文件分开):
//This class instantiates a message object for populating the
//vector arrays.
#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include "Message.h"
using namespace std;
//Function Prototypes:
//Message inbox(string, string, string);
//Message sent(string, string, string);
void viewInboxMessage();
Message::Message(string address, string subject, string text)
{
setAddress(address);
setSubject(subject);
setMessageText(text);
}
//START OF GETTERS AND SETTERS
void Message::setAddress(string addr)
{
address = addr;
}
string Message::getAddress()
{
return address;
}
void Message::setSubject(string subj)
{
subject = subj;
}
string Message::getSubject()
{
return subject;
}
void Message::setMessageText(string msgTxt)
{
text = msgTxt;
}
string Message::getMessageText()
{
return text;
}
//END OF GETTERS AND SETTERS
//Main Menu option 1 selected:
// print list of all messages to the console
void viewInbox()
{
cout << "You have " << inbox.size << " new messages.\n";
std::cout << "Index Subject" << '\n';
for (size_t i = 0; i < inbox.size(); ++i)
{
std::cout << i << " : " << inbox[i].subject << '\n';
}
}
//write a function that will display as many case break switch choices
//as there are messages in the inbox
//print selected message to console:
void viewInboxMessage(size_t index)
{
if (index < inbox.size())//error: identifier "inbox" is not defined.
{
std::cout << "Message Number : " << index << '\n';
std::cout << "sender: " << inbox[index].address << '\n';
std::cout << "subject: " << inbox[index].subject << '\n';
std::cout << "message: " << '\n';
std::cout << inbox[index].text << '\n';
}
}
//Main Menu option 3 selected:
Message newMessage(string address, string subject, string compose)
{
cout << "Please enter recipient email address.\n";
cin >> address;
cout << "Please enter a subject for your message.\n";
cin >> subject;
cout << "Go ahead and type your message, and hit send when you're finished.\n";
cin >> compose;
vector<Message> viewSent(0)++;//add composed message to sent messages list after user sends it.
}
带主函数的文件:
//This class contains the main function and the main menu.
#include <iostream>
#include <iomanip>
#include <string>
#include "Message.h"
using namespace std;
int main()
{
//Message inbox[10];
vector<Message> inbox;
Message sent[10];
string sender;
string subject;
int numMessages;//number of messages in inbox
int choice = 0;
string verify = "Cartman2010";
string userName;
string address;
string password;
cout << "please enter your login username.\n";
cin >> userName;
cout << "please enter your email address.\n";
cin >> address;
cout << "please enter your password.\n";
cin >> password;
if (password != verify)
{
cout << "Invalid password. Please re-enter your password.\n";
cin >> password;
}
else
cout << "Hello " << userName << " Welcome to your mailbox. Please select an option:\n";
//After easy login and greeting, the user needs to select an option form the main menu:
cout << "1. View Your Inbox\n";
cout << "2. View Sent Messages\n";
cout << "3. Send A Message\n";
cout << "4. Quit\n";
cin >> choice;
switch (choice)
{
case 1: Message viewInbox;//access messages (and number of messages) from vector inbox
break;
case 2: Message viewSent;//view the messages you've sent (and how many) from vector
break;
case 3: Message newMessage;
break;
case 4: exit();
}
return 0;
}
我应该在哪里分配3个字符串,&#34;地址&#34;,&#34;主题&#34;和&#34;文字&#34;进入Message对象的向量元素?
答案 0 :(得分:1)
你说你的Message
对象应该有3个字符串属性,但你的Message
有6.你也似乎试图让整个邮件服务器适合你的Message类。我不确定你的整个标准,但也许这种结构可能会有所帮助:
struct Message
{
std::string address; // sender/recipient
std::string subject;
std::string text; // actual message
// compose message functions here
};
class EmailServer
{
private:
std::vector<Message> inbox;
std::vector<Message> sent;
public:
// mail server functions here ...
// print list of all messages to the console
void viewInbox()
{
std::cout << "Index Subject" << '\n';
for(size_t i = 0; i < inbox.size(); ++i)
{
std::cout << i << " : " << inbox[i].subject << '\n';
}
}
// print a specific message to the console
void viewInboxMessage(size_t index)
{
if(index < inbox.size())
{
std::cout << Message Number: " << index << '\n';
std::cout << "sender: " << inbox[index].address << '\n';
std::cout << "subject: " << inbox[index].subject << '\n';
std::cout << "message: " << '\n';
std::cout << inbox[index].text << '\n';
}
}
};