C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

segmentation fault

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Chris
Guest





PostPosted: Sun Aug 13, 2006 9:10 am    Post subject: segmentation fault Reply with quote



I'm compiling the following code with GCC 4.1.0 (on fedora 6 I think).
It compiles fine, then when I run it it simply prints "segmentation
fault".

int main() {
vector<vector<string> > yo;
yo[0][0] = "hello";
cout << yo[0][0];

return 0; }

1. What's causing this?

I'm using the object "vector<vector<string> >" so I can split a string
into lines, then split the lines into words so that
"hello there\nTommy Boy"
becomes
[ ["hello", "there"], ["Tommy", "Boy"] ]

2. Should I do this a different way?

Thanks for any help!
Back to top
Ian Collins
Guest





PostPosted: Sun Aug 13, 2006 9:10 am    Post subject: Re: segmentation fault Reply with quote



Chris wrote:
Quote:
I'm compiling the following code with GCC 4.1.0 (on fedora 6 I think).
It compiles fine, then when I run it it simply prints "segmentation
fault".

int main() {
vector<vector<string> > yo;
yo[0][0] = "hello";
cout << yo[0][0];

return 0; }

1. What's causing this?

Surely this must be an FAQ by now, if not, look back though the past

week or so of postings. You can't use operator [] on an empty vector.

--
Ian Collins.
Back to top
Jim Langston
Guest





PostPosted: Sun Aug 13, 2006 9:10 am    Post subject: Re: segmentation fault Reply with quote



"Chris" <chrispatton (AT) gmail (DOT) com> wrote in message
news:1155444123.919593.158580 (AT) m79g2000cwm (DOT) googlegroups.com...
Quote:
I'm compiling the following code with GCC 4.1.0 (on fedora 6 I think).
It compiles fine, then when I run it it simply prints "segmentation
fault".

int main() {
vector<vector<string> > yo;
yo[0][0] = "hello";
cout << yo[0][0];

return 0; }

1. What's causing this?

I'm using the object "vector<vector<string> >" so I can split a string
into lines, then split the lines into words so that
"hello there\nTommy Boy"
becomes
[ ["hello", "there"], ["Tommy", "Boy"] ]

2. Should I do this a different way?

Thanks for any help!

Your yo vector is empty. You need to insert into it before you can use it.

std::vector mama;
mama.push_back( "hello" );
yo.push_back( mama );
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.