What is the error with this code?

J

John

Hi all;

I write my first code using vector and list. When I run it,
segmentation fault.
Try to debug it, but it can not pass linking with -g option.
What is the error with it?

Thanks a lot.

John

----------------------------------------------------------
The code is attached:

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

using namespace std;

typedef struct{
int ID;
double time;
}node;

vector<node> myvector(){//Define a function that return a vector,
//can I do this?
vector< node > v;
for(int i = 1; i < 7; i++){
v.ID = i;
v.time = i * 1.1;
}
cout<<"v:"<<endl;
for(int i = 0; i < v.size(); ++i){
cout<<"i:"<<i<<" ID:"<< v.ID <<" time:"<<v.time <<endl;
}

return v;
}

void mylist(list< int > &l){//call-by-reference to bring the list
//back to main()
for(int i = 10; i < 16; i++){
l.push_back(i);
}
cout<<"l:"<<endl;
list<int>::iterator pos;
for(pos = l.begin(); pos != l.end(); ++pos){
cout<<"pos:"<<*pos<<endl;
}

}

int main()
{
vector< node > v1;
v1 = myvector();//get the vector from myvector().

cout<<"v1:"<<endl;

for(int i = 0; i < v1.size(); ++i){
cout<<"i1:"<<i<<" ID1:"<< v1.ID <<" time1:"<<v1.time <<endl;
}

list< int > l1;
mylist(l1);
cout<<"l1:"<<endl;

list< int >::iterator pos;

for(pos = l1.begin(); pos != l1.end(); ++pos){
cout<<"pos1:"<<*pos<<endl;
}

list< int* > l2;//declare a list of int pointer, can I do this?
int a1 = 100;
int a2 = 200;
l2.push_back(&a1);
l2.push_back(&a2);

cout<<"&a1:"<<&a1<<" &a2:"<<&a2<<endl;
cout<<"l2-a1:"<<l2.front()<<" l2-a2:"<<l2.back()<<endl;

list< int* >::iterator pos1;
pos1 = l2.begin();

cout<<"a1:"<<*(*pos1)<<endl;
cout<<"a2:"<<*(l2.end());

}



---------------------------------------------------
Error message, when I use: g++ -o -g vector vector.cc
$g++ -o -g vector vector.cc
vector(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/crt1.o(.rodata+0x0): first defined here
vector(.init+0x0): In function `_init':
: multiple definition of `_init'
/usr/lib/crti.o(.init+0x0): first defined here
vector(.text+0x0): In function `_start':
: multiple definition of `_start'
/usr/lib/crt1.o(.text+0x0): first defined here
vector(.fini+0x0): In function `_fini':
: multiple definition of `_fini'
/usr/lib/crti.o(.fini+0x0): first defined here
vector(*ABS*+0x804d84c): multiple definition of
`_GLOBAL_OFFSET_TABLE_'
/usr/lib/crt1.o(.got.plt+0x0): first defined here
vector(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/crt1.o(.rodata+0x4): first defined here
vector(.data+0x0): In function `__data_start':
: multiple definition of `__data_start'
/usr/lib/crt1.o(.data+0x0): first defined here
/tmp/cctWTqCU.o(.text+0x23c): In function `mylist(list<int,
allocator<int> > &)':
: multiple definition of `mylist(list<int, allocator<int> > &)'
vector(.text+0x258): first defined here
/usr/bin/ld: Warning: size of symbol `mylist(list said:
&)' changed from 270 to 278 in /tmp/cctWTqCU.o
/tmp/cctWTqCU.o(.text+0x354): In function `main':
: multiple definition of `main'
vector(.text+0x368): first defined here
/usr/bin/ld: Warning: size of symbol `main' changed from 1457 to 1518
in /tmp/cctWTqCU.o
/usr/lib/crt1.o(.dynamic+0x0): multiple definition of `_DYNAMIC'
collect2: ld returned 1 exit status

$
 
D

Daniel T.

I write my first code using vector and list. When I run it,
segmentation fault.
Try to debug it, but it can not pass linking with -g option.
What is the error with it?

With the one line I changed, the program ran and proceded to output a
bunch of stuff, but I have no idea if the stuff it output is the stuff
you wanted it to output.
----------------------------------------------------------
The code is attached:

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

using namespace std;

typedef struct{
int ID;
double time;
}node;

vector<node> myvector(){//Define a function that return a vector,
//can I do this?

Yes, you can do that.
vector< node > v;

Note, at this point 'v' has no elements in it. With the code below, you
are trying to access elements that don't exist. Try this instead:

vector said:
for(int i = 1; i < 7; i++){
v.ID = i;
v.time = i * 1.1;
}
cout<<"v:"<<endl;
for(int i = 0; i < v.size(); ++i){
cout<<"i:"<<i<<" ID:"<< v.ID <<" time:"<<v.time <<endl;
}

return v;
}

void mylist(list< int > &l){//call-by-reference to bring the list
//back to main()
for(int i = 10; i < 16; i++){
l.push_back(i);
}
cout<<"l:"<<endl;
list<int>::iterator pos;
for(pos = l.begin(); pos != l.end(); ++pos){
cout<<"pos:"<<*pos<<endl;
}

}

int main()
{
vector< node > v1;
v1 = myvector();//get the vector from myvector().

cout<<"v1:"<<endl;

for(int i = 0; i < v1.size(); ++i){
cout<<"i1:"<<i<<" ID1:"<< v1.ID <<" time1:"<<v1.time <<endl;
}

list< int > l1;
mylist(l1);
cout<<"l1:"<<endl;

list< int >::iterator pos;

for(pos = l1.begin(); pos != l1.end(); ++pos){
cout<<"pos1:"<<*pos<<endl;
}

list< int* > l2;//declare a list of int pointer, can I do this?
Yes.

int a1 = 100;
int a2 = 200;
l2.push_back(&a1);
l2.push_back(&a2);

cout<<"&a1:"<<&a1<<" &a2:"<<&a2<<endl;
cout<<"l2-a1:"<<l2.front()<<" l2-a2:"<<l2.back()<<endl;

list< int* >::iterator pos1;
pos1 = l2.begin();

cout<<"a1:"<<*(*pos1)<<endl;
cout<<"a2:"<<*(l2.end());

}
 
A

Andrew Koenig

typedef struct{
int ID;
double time;
}node;

Bad form--it's much better to write

struct node {
int ID;
double time;
};
vector<node> myvector(){//Define a function that return a vector,
//can I do this?
Yes.
vector< node > v;
for(int i = 1; i < 7; i++){
v.ID = i;
v.time = i * 1.1;
}


Here is your problem: v has no elements, but you are trying to access those
nonexistent elements. You should write

vector<node> v;

for (int i = 1; i < 7; i++) {
node n;
n.ID = i;
n.time = i * 1.1;
v.push_back(n);
}
 
R

Rolf Magnus

You're telling your g++ to compile 'vector.cc', link a file called
'vector' to it and write the resulting executable to a file called
'-g'. I guess you actually meant:

g++ -g -o vector vector.cc
 
J

jeffc

Andrew Koenig said:
vector< node > v;
for(int i = 1; i < 7; i++){
v.ID = i;
v.time = i * 1.1;
}


Here is your problem: v has no elements, but you are trying to access those
nonexistent elements. You should write

vector<node> v;

for (int i = 1; i < 7; i++) {
node n;
n.ID = i;
n.time = i * 1.1;
v.push_back(n);
}


I remember the confusing thing about vectors vs. maps was that v = ...
would not create a new member, but m = ... would create a new member (v-
vector, m- map).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top