468,512 Members | 1,431 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

how to initialize a vector in the constructor

hi, all
Suppose I have the following header file:

#include <vector>
using namespace std;

class Test{
public:
~Test();
Test();
private:
vector<intv;
vector<vector<int vv;
}

Then in the Test.cpp, I have

Test::Test() : v(0), vv(0){}

Am I initialize the vector correct? At the time when the constructor is
excuted, I don't have the number to fill the vector yet, so I put them
to size of 0. (It seems I initialize them to null, but null is not
initialized, right?) Please correct me if I am doing wrong. I got
strange run time errors which indicating some of the parameters are not
initialized correctly. Thanks for help.

zl2k

Aug 7 '06 #1
2 20886
* zl2k:
hi, all
Suppose I have the following header file:

#include <vector>
using namespace std;
Don't put 'using namespace std;' in a header file.

It can also be a good idea to restrict its usage elsewhere.

See the FAQ item "Should I use using namespace std in my code?",
currently at <url:
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5>.

class Test{
public:
~Test();
Test();
private:
vector<intv;
vector<vector<int vv;
}

Then in the Test.cpp, I have

Test::Test() : v(0), vv(0){}

Am I initialize the vector correct?
Yes. You initialize both vectors to zero size. I.e. they're empty.

At the time when the constructor is
excuted, I don't have the number to fill the vector yet, so I put them
to size of 0. (It seems I initialize them to null, but null is not
initialized, right?) Please correct me if I am doing wrong. I got
strange run time errors which indicating some of the parameters are not
initialized correctly. Thanks for help.
See the FAQ item "How do I post a question about code that doesn't work
correctly?", currently at <url:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 7 '06 #2
In article <11*********************@b28g2000cwb.googlegroups. com>,
"zl2k" <kd*******@gmail.comwrote:
hi, all
Suppose I have the following header file:

#include <vector>
using namespace std;

class Test{
public:
~Test();
Test();
private:
vector<intv;
vector<vector<int vv;
}

Then in the Test.cpp, I have

Test::Test() : v(0), vv(0){}

Am I initialize the vector correct?
That's one way to do it.
At the time when the constructor is
excuted, I don't have the number to fill the vector yet, so I put them
to size of 0. (It seems I initialize them to null, but null is not
initialized, right?) Please correct me if I am doing wrong. I got
strange run time errors which indicating some of the parameters are not
initialized correctly. Thanks for help.
Chances are, you are trying to access elements in one of the vectors
that don't exist yet.
Aug 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by cppaddict | last post: by
2 posts views Thread by Christian Christmann | last post: by
5 posts views Thread by Jim Langston | last post: by
9 posts views Thread by www.brook | last post: by
18 posts views Thread by toton | last post: by
9 posts views Thread by Baby Lion | last post: by
9 posts views Thread by Steven Woody | last post: by
11 posts views Thread by Bob Altman | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.