473,466 Members | 1,290 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Manage a dynamic vector in a structure

Hi,
My problem is the manage of a dynamic vector in a list of structures.

In the .hpp file I define the struct and the vector v.

typedef struct Word
{ char* Name;
vector<int> v;
Word* Next;
Word()
{Next=NULL;
Name=NULL;
}
};
typedef Word* ListPtr;

In the .cpp file I create a struct and I would to manage the vector inside
this structure,, like ie: clear it.
The point, in the .cpp file, where I define the struct is....
CList:: CList()
{
int i;
Head = new Word;
Tail=Head;
CurrentPtr = Head;
v.pushback(10); //It return me an error about the undeclared function
pushback
for (i=0;i<10;i++)
Tail->v[i]=1; //It return me a Segmentation Fault
}
I'm unable to make everything with this vector.
Can you help me?

Thanks
Daniele

"Il saggio non schiaccia gli
altri con la sua superioritÃ*; non
li umilia mettendo in rilievo la
loro incapacitÃ*."
Confucio
Jul 22 '05 #1
3 5004

"Daniele" <se_lachiedi@te_la_dico.it> wrote in message
news:op**************@localhost.localdomain...
Hi,
My problem is the manage of a dynamic vector in a list of structures.

In the .hpp file I define the struct and the vector v.

typedef struct Word
{ char* Name;
vector<int> v;
Word* Next;
Word()
{Next=NULL;
Name=NULL;
}
};
typedef Word* ListPtr;

In the .cpp file I create a struct and I would to manage the vector inside
this structure,, like ie: clear it.
The point, in the .cpp file, where I define the struct is....
CList:: CList()
{
int i;
Head = new Word;
Tail=Head;
CurrentPtr = Head;
v.pushback(10); //It return me an error about the undeclared function
pushback
for (i=0;i<10;i++)
Tail->v[i]=1; //It return me a Segmentation Fault
}
I'm unable to make everything with this vector.
Can you help me?


Yes, you've got lots of things wrong.

1) Its push_back, not pushback

2) push_back(10) does not add 10 items to the vector, it adds a single
integer 10 to the vector.

3) So it looks to me that what you want is

for (i=0;i<10;i++)
Tail->v.push_back(1);

but its hard to be sure.

john
Jul 22 '05 #2
Daniele wrote:
My problem is the manage of a dynamic vector in a list of structures.

In the .hpp file I define the struct and the vector v.

Remove the 'typedef' below.
typedef struct Word
{ char* Name;
vector<int> v;
Word* Next;
Word()
{Next=NULL;
Name=NULL;
}
};
typedef Word* ListPtr;

In the .cpp file I create a struct and I would to manage the vector
inside this structure,, like ie: clear it.
The point, in the .cpp file, where I define the struct is....
CList:: CList()
{
int i;
Head = new Word;
Tail=Head;
CurrentPtr = Head;
v.pushback(10); //It return me an error about the undeclared function
pushback
The member function name is "push_back". Please RTFM.
for (i=0;i<10;i++)
Tail->v[i]=1; //It return me a Segmentation Fault
If it can't compile, what segmentation fault are you talking about?
How are you able to run a program that didn't compile?
}
I'm unable to make everything with this vector.
Can you help me?


I am not sure.

V
Jul 22 '05 #3
Daniele wrote:
Hi,
My problem is the manage of a dynamic vector in a list of structures.

In the .hpp file I define the struct and the vector v.

typedef struct Word
{ char* Name;
vector<int> v;
Word* Next;
Word()
{Next=NULL;
Name=NULL;
}
};
typedef Word* ListPtr;

It looks like you have a C background; in C++ you don't need the
typedef; struct Word {...}; would do just fine. Also you would probably
better of using std::string for the Name member. Working with
std::string objects is much easier than using char pointers.
In the .cpp file I create a struct and I would to manage the vector
inside this structure,, like ie: clear it.
The point, in the .cpp file, where I define the struct is....
CList:: CList()
{
int i;
Head = new Word;
Tail=Head;
CurrentPtr = Head;
v.pushback(10); //It return me an error about the undeclared function
pushback
pushback() is not a member of std::vector, you probably wantpush_back().
Also you have to specify in which object v is, I assume that would be Head:

Head->v.push_back(10);
for (i=0;i<10;i++)
Tail->v[i]=1; //It return me a Segmentation Fault
}


Most likely there aren't 10 elements in the vector. When i>=v.size()
anything may happen including a segmentation fault. It is hard to say
what goes wrong when seeing only code snippets. If you post minimal yet
complete (compilable) code that demonstrates the problem you are having,
the people here would be able to help you better.

It seems you are trying to implement a linked list, have you heard about
the std::list<> class?

As a final note I think a good beginners book like "Accelerated C++"
(http://www.acceleratedcpp.com/) would be very useful for you.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 22 '05 #4

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

Similar topics

6
by: Vasileios Zografos | last post by:
Hello, I have a function that generates some values (e.g. vertices in 2d space) the number of which I dont know. So, it could generate 20 vertices, 100 vertices, or even 1 vertex. void...
1
by: Richard | last post by:
Define a record structure to hold a set of test scores, number of tests, and the average score. The number of records are to be determined at runtime. Also the number of tests for each record is...
7
by: NeeZee | last post by:
hi, im new to this group and to the c++-language. the problem: i need an array-like class/data structure whose dimension can be set dynamically. i noticed that there is a vector-class in STL, but...
1
by: Russell Mangel | last post by:
Sorry about the Cross-Post, I posted my question in the wrong group. Hello, What is the simplest way to create a dynamic collection (during run-time), using basic C (Struct data types). Since...
12
by: googlinggoogler | last post by:
Hi, Im new to C++ and trying to self teach myself whilst I sit at my unentertaining day job (thought i'd put that across before im accused of cheating on my homework, im 45...) Anyway I'm...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
4
by: =?Utf-8?B?UHVjY2E=?= | last post by:
The function that I'm trying to call through DLLImport has a parameter that has a C code's vector's Itrator to a structure. I Have marshalled the structure in C# but how do I do the C type...
14
by: broli | last post by:
In my project it is getting really messy to take care of all the headers..for eg. right now I have following modules in my project - vector.c vector.h test.c reader.h reader.c vector.h
5
by: cham | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.