Manage a dynamic vector in a structure

D

Daniele

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=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
 
V

Victor Bazarov

Daniele said:
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=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
 
P

Peter van Merkerk

Daniele said:
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=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.
 
J

John Harrison

Daniele said:
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=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
 

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

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top