0

my C++ might include multi-error... the first one is how to add struct element in a vector

my code will be like this:

#include<iostream>
#include<vector>
using namespace std;
struct eMailMsg {
string to; // i.e. "professor@stanford.edu"
string from; // i.e. "student@stanford.edu"
string message; // body of message
string subject; // i.e. "CS106 Rocks!"
int date; // date email was sent
int time; // time email was sent
};

int main(){
    vector <eMailMsg> mailVector;
    mailVector[0]={"professor@stanford.edu","student@stanford.edu","body of message",4,16};
    for( std::vector<eMailMsg>::const_iterator i = mailVector.begin(); i != mailVector.end(); ++i)
    std::cout << *i << ' ';
    system("pause");
    return 0;
}
BenSeedangie
  • 47
  • 3
  • 10
  • 1
    What is your question exactly? – Caesar Jan 05 '14 at 00:37
  • mailVector[0]={"professor@stanford.edu","student@stanford.edu","body of message",4,16}; This statement is correct or not? @Mohammed Majeed – BenSeedangie Jan 05 '14 at 00:38
  • @BenSeedangie You haven't allocated the vector with elements. Thus accessing the address `&mailVector[0]` is undefined behavior. – David G Jan 05 '14 at 00:39
  • What is `system("pasuse");`? ;) – leemes Jan 05 '14 at 00:41
  • Also `cout << *i` can't work since you don't define how it should be printed (i.e. there is no [overload for `operator<<`](http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/)) – leemes Jan 05 '14 at 00:43
  • 0x499602D2 what the exact syntax look like if I would like to add those information to mailVector? should I use push_back?? – BenSeedangie Jan 05 '14 at 00:43

2 Answers2

5

you put something in a vector with the .push_back() method, so for starters:

mailVector.pushBack(...)

code:

eMailMsg myMailMsg = {"professor@stanford.edu","student@stanford.edu","body of message", 4, 16};
mailVector.pushBack(myMailMsg);
ivpavici
  • 1,117
  • 2
  • 19
  • 30
3
  1. To add an element to a vector, there is std::vector::push_back:

    mailVector.push_back({"professor@stanford.edu","student@stanford.edu","body of message",4,16});
    

    The braces construct a new eMailMsg when passed to the function. See a working code here.

    Your syntax is used to replace an element with a new value (assignment).

    Since C++11 we also have std::vector::emplace_back which is slightly more efficient. The function push_back copies or moves an existing object into the vector. But emplace_back constructs it "in place" with the provided constructor arguments. It is provided the constructor arguments:

    mailVector.emplace_back("professor@stanford.edu","student@stanford.edu","body of message",4,16);
    

    A third way (not recommended) is to first resize the vector to a given size (std::vector::resize, or initially in the constructor) and then using the assignment syntax you've used in your code.

  2. You forgot one initializer (argument) for the construction of an eMailMsg object: the subject.

  3. To print a value of a custom type (in your case an eMailMsg) using std::cout, you need to overload the streaming operator. A typical implementation uses the provided stream to print several things, and then returns the stream for concatenation of multiple values in one line:

    std::ostream& operator<<(std::ostream &out, eMailMsg &mail) {
        // Print only "to" and "from" for demonstration.
        return out << "To: " << mail.to << "\nFrom: " << mail.from << "\n";
    }
    
  4. Never use system("pause");.

Community
  • 1
  • 1
leemes
  • 44,967
  • 21
  • 135
  • 183
  • Actually, I would like to print the elements in vector mailVector. It seems that you code is work for printing the struct content. Correct me, if I was wrong. THank you!!! – BenSeedangie Jan 05 '14 at 01:22
  • I don't understand what you want to print. The elements in the vector **are** structs, and printing them only works if **you** define how it should be done. My answer demonstrates this by printing the receiver and sender address in two lines, but you might want to have something else... – leemes Jan 05 '14 at 01:24