How to instantiate a Vector b4 using it....

Hi, I've a simple class MailBox, in the .h file I've declared a vector vector<Message> _emails;. The class includes a method print() which seeks to iterate over each element in the vector, however I'm gettin d following error when i run my main() MailBox.cpp:25: instantiated from here which obviously means that im trying, however I dont know how or where to instantiate the Vector so I can iterate over it, and hope someone could show me how to do it in code... ta...

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
34
35
/*
 * MailBox.cpp
 *
 *  Created on: 26-Nov-2008
 *      Author: Patrick
 */

#include "MailBox.h"

MailBox::MailBox() :
	_size(0){
	// TODO Auto-generated constructor stub

}
void MailBox::addMessage(Message aMessage) {
	_emails.push_back(aMessage);
	_size++;
}
int MailBox::getSize() {
	return _size;
}

void MailBox::print() const {
	vector<Message>::iterator it;
		for (it = _emails.begin(); it != _emails.end(); it++) {
			//set curent to the current Employee
			Message current = *it;
			//and invoke its print method
			current.print();
		}
}

MailBox::~MailBox() {
	// TODO Auto-generated destructor stub
}

Ok just found out that I can do this by changing the method body of print() to
1
2
3
	for(int i= 0; i < _size; i++) {
	            Message current = _emails.at(i);
	            current.print();

However Id still b interested in knowing how to instantiate a vector before u use it if anyone know how thanks
Last edited on
Can you please give us the MailBox.h file contents too.
Last edited on
yeah no prob sorry..
.
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
/*
 * MailBox.h
 *
 *  Created on: 26-Nov-2008
 *      Author: Patrick
 */

#ifndef MAILBOX_H_
#define MAILBOX_H_
#include "Message.h"
#include <vector>
using namespace std;
class MailBox {
	vector<Message> _emails;
	int _size;
public:
	MailBox();
	void addMessage(Message aMessage);
	int getSize();
	vector<Message>getMessages();
	void print() const;
	virtual ~MailBox();
};

#endif /* MAILBOX_H_ */ 
You do know that Message current = _emails.at(i) copies the object which, judging by the name, probably implies costly string copies. You could also do this: _emails[i].print()

MailBox.cpp:25: instantiated from here
This is just additional information about an error, not an error itself. It gives context to some error message above this line. Read a little more carefully and you'll see what the error is really about. Probably something related to _emails.
Instantiating an object just means creating it. A necessary step in order to do anything with it.

EDIT: std::vector::size() tells you the size of the vector.
Last edited on
Probably something to do with it being a const function.
and the iterator is not a const iterator and is trying to use the _emails vector.
Thanks lads... some good points to bear in mind helios like d direct referene or whatever u call it i.e. _emails[i].print()....

sorry for our initail disagreement over pointers to vectors d other nite :)

Topic archived. No new replies allowed.