Work with Arrays

F

Frank Liebelt

Hi

I hope my english ist good enough to explain what my problem is.

I want to create a array with two dimensions. Each index should have a
second dimension with three indexes.
Like: myarr[0][0-2] , myarr[1][0-2] and so on...

The size of the first dimension is unknown the second ist allways three.

In Detail:

I read an XML File and save all Values in an char* Array:

I tried to use a vector for this problem.
Within the headerfile i wrote: vector<char> arrSpec;

....
int listPos = -1;

// This resize works
arrSpec.resize(XMLCount)

while ( element ) {
char *id = NULL;
char *name = NULL;
char *path = NULL;

listPos++;

id = xmlGetAttribute(element, _id);
name = xmlGetAttribute(element, _name);
path = xmlGetAttribute(element, _path

arrSpec[listPos].resize(3);
arrSpec[listPos][0].push_back(id);
arrSpec[listPos][1].push_back(name);
arrSpec[listPos][2].push_back(path);
....

Now the Problem.
-> arrSpec[listPos].resize(3);
Wont work. I got the following compiler error:

---
test.cpp:222: error: request for member `resize' in `(((std::vector<char,
std::allocator<char> >*)((TestClass*)this)) + 68u)->std::vector<_Tp,
_Alloc>::eek:perator[] [with _Tp = char, _Alloc = std::allocator<char>]
(((unsigned int)listPos))', which is of non-class type `char'
---

The next three line have this compiler error:
invalid types `char[int]' for array subscript

I think this is cause the resize faild. But not sure.

So i need any help to get this done.

Frank
 
M

Martin T.

Frank said:
Hi

I hope my english ist good enough to explain what my problem is.

I want to create a array with two dimensions. Each index should have a
second dimension with three indexes.
Like: myarr[0][0-2] , myarr[1][0-2] and so on...

The size of the first dimension is unknown the second ist allways three.

In Detail:

I read an XML File and save all Values in an char* Array:
If you want to work with strings use the std::string class
I tried to use a vector for this problem.
Within the headerfile i wrote: vector<char> arrSpec;
This defines a vector of characters.
What you want is probably:
vector<std::string> for a list of strings
or maybe vector said:
...
int listPos = -1;

// This resize works
arrSpec.resize(XMLCount)

while ( element ) {
char *id = NULL;
char *name = NULL;
char *path = NULL;

listPos++;

id = xmlGetAttribute(element, _id);
name = xmlGetAttribute(element, _name);
path = xmlGetAttribute(element, _path

assuming that xmlGetAttribute returns a char* you have to make sure who
is responsible for free'ing the memory!
arrSpec[listPos].resize(3);

This fails because your original specification defines the lement
arrSpec[listPos] as a char, and that has no resize method.
arrSpec[listPos][0].push_back(id);
arrSpec[listPos][1].push_back(name);
arrSpec[listPos][2].push_back(path);
...

Maybe better:
if(id) {
arrSpec[listPos][0].push_back(id);
}


Hope this helps, br,
Martin
 
E

Eric.Malenfant


Hi Frank,
I hope my english ist good enough to explain what my problem is.

I hope mine is good enough to answer :)
I want to create a array with two dimensions. Each index should have a
second dimension with three indexes.
Like: myarr[0][0-2] , myarr[1][0-2] and so on...

The size of the first dimension is unknown the second ist allways three.
[snip]

Within the headerfile i wrote: vector<char> arrSpec; [snip]

Now the Problem.
-> arrSpec[listPos].resize(3);
Wont work. I got the following compiler error:

The return type of operator[] is the vector's element type which is,
in the present case, "char". "char" does not have a "resize" member
function.
The next three line have this compiler error:
invalid types `char[int]' for array subscript

And "char" has no operator[] either.

What you're trying to achieve is not entirely clear to me, but do you
really need to access the sub-elements with this double indexing
scheme? Looking at your code below, it seems that sub-index 0 is
always "id", 1 is "name" and 2 is "path". So, for example, to get the
name of the n-th element, would'nt this:
arrSpec[n].name
be more expressive than:
arrSpec[n][1]
?

If yes, then a solution could be: create a type for your elements,
say:
struct Element
{
char* id;
char* name;
char* path;
};
and store you data in a vector<Element>

If you really need to use a double-indexing scheme, you could use
boost::array, and define your vector as:
std::vector<boost::array<char*, 3> >

HTH,

Éric Malenfant
 
F

Frank Liebelt

Hi
If yes, then a solution could be: create a type for your elements, say:
struct Element
{
char* id;
char* name;
char* path;
};
and store you data in a vector<Element>

Thanks, thats much easier to access the elements by name.
Hours of work and then its so simple.

Frank
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top