473,750 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the error with this code?

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[i].ID = i;
v[i].time = i * 1.1;
}
cout<<"v:"<<end l;
for(int i = 0; i < v.size(); ++i){
cout<<"i:"<<i<< " ID:"<< v[i].ID <<" time:"<<v[i].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:"<<end l;
list<int>::iter ator 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:"<<en dl;

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

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

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(&a 1);
l2.push_back(&a 2);

cout<<"&a1:"<<& a1<<" &a2:"<<&a2<<end l;
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+0x 0): In function `_init':
: multiple definition of `_init'
/usr/lib/crti.o(.init+0x 0): first defined here
vector(.text+0x 0): In function `_start':
: multiple definition of `_start'
/usr/lib/crt1.o(.text+0x 0): first defined here
vector(.fini+0x 0): In function `_fini':
: multiple definition of `_fini'
/usr/lib/crti.o(.fini+0x 0): first defined here
vector(*ABS*+0x 804d84c): 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+0x 0): In function `__data_start':
: multiple definition of `__data_start'
/usr/lib/crt1.o(.data+0x 0): first defined here
/tmp/cctWTqCU.o(.tex t+0x23c): In function `mylist(list<in t,
allocator<int> > &)':
: multiple definition of `mylist(list<in t, allocator<int> > &)'
vector(.text+0x 258): first defined here
/usr/bin/ld: Warning: size of symbol `mylist(list<in t, allocator<int>
&)' changed from 270 to 278 in /tmp/cctWTqCU.o

/tmp/cctWTqCU.o(.tex t+0x354): In function `main':
: multiple definition of `main'
vector(.text+0x 368): 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

$
Jul 22 '05 #1
4 1737
jo*********@yah oo.com (John) wrote:
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< node > v(7); for(int i = 1; i < 7; i++){
v[i].ID = i;
v[i].time = i * 1.1;
}
cout<<"v:"<<end l;
for(int i = 0; i < v.size(); ++i){
cout<<"i:"<<i<< " ID:"<< v[i].ID <<" time:"<<v[i].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:"<<end l;
list<int>::iter ator 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:"<<en dl;

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

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

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(&a 1);
l2.push_back(&a 2);

cout<<"&a1:"<<& a1<<" &a2:"<<&a2<<end l;
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());

}

Jul 22 '05 #2
> 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[i].ID = i;
v[i].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);
}

Jul 22 '05 #3
John wrote:
---------------------------------------------------
Error message, when I use: g++ -o -g vector vector.cc


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

Jul 22 '05 #4

"Andrew Koenig" <ar*@acm.org> wrote in message
news:8A******** ************@bg tnsc05-news.ops.worldn et.att.net...
vector< node > v;
for(int i = 1; i < 7; i++){
v[i].ID = i;
v[i].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[i] = ...
would not create a new member, but m[i] = ... would create a new member (v-
vector, m- map).
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2599
by: kaiwing18 | last post by:
Hi , I have a problem relate to java and database. Could anyone answer me ?Please see the following code. import java.sql.*; public class Result { public static void main(String args) {
3
2966
by: fastwings | last post by:
mm the code //////makemenu.h//// class menu { public: int op; pmenu(int op,int sub = 0) { switch op {
72
5884
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for example, between a C/C++ programmers with a few weeks of experience and a C/C++ programmer with years of experience. You don't really need to understand the subtle details or use the obscure features of either language
8
17556
by: aling | last post by:
Given the bit field struct: int main() { union { struct { unsigned short s1 : 4; unsigned short s2 : 3;
51
13383
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this wrong????? Function x select case z
4
2471
by: atv | last post by:
Whatis the proper way to handle errors from function calls? For example, i normally have a main function, with calls to mine or c functions. Should i check for errors in the functions called themselves, or should i return a code to main and handle the error there? If i don't return them to main, except for the structure, what use is the main function except for calling functions?
46
4232
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do believe MSFT should do to improve C#, however. I know that in the "Whidbey" release of VS.NET currently
13
5053
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
10
3233
by: Protoman | last post by:
Could you tell me what's wrong with this program, it doesn't compile: #include <iostream> #include <cstdlib> using namespace std; class Everything { public: static Everything* Instance()
5
13496
by: =?GB2312?B?17/HvyBaaHVvLCBRaWFuZw==?= | last post by:
Hi, I would like to have someone comments on what's the best practice defining error codes in C. Here's what I think: solution A: using enum pros: type safe. better for debug (some debugger will show the name not only the value) cons: enum can not be forward declared which makes all error codes
0
8999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9338
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6803
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4712
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.