473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Work with Arrays

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<chararrSpec;

....
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>::operator[] [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

Jun 27 '08 #1
3 1667
Frank Liebelt wrote:
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<chararrSpec;
This defines a vector of characters.
What you want is probably:
vector<std::stringfor a list of strings
or maybe vector< vector<std::string for a list of lists of strings
>
...
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
Jun 27 '08 #2
On Apr 30, 9:56 am, Frank Liebelt <ecos...@hotmail.comwrote:
Hi
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<chararrSpec;
[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
Jun 27 '08 #3
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
Jun 27 '08 #4

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

Similar topics

1
by: Reed Law | last post by:
I have the exact same data in two arrays, but only the array created like so will work: $spaw_imglibs = array( array( 'value' => '/youth/pics/Member pics/', 'text' => 'Member pics', ),...
17
by: middletree | last post by:
OK, you pros out there are rolling your eyes at the subject line, but I have never had to use arrays before. The scenario: ASP Intranet app keeps track of trouble tickets for a tech support group....
11
by: Laphan | last post by:
Hi All I'm using .getRows() with a local var array instead of doing a recursive loop so that I'm being a good ASP newvbie and closing my object i/o's (the recordset in this case) as quick as...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
6
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
55
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable...
4
by: crochunter | last post by:
Hi, I want to read values from a text files from specified fields and use them as values to fill my methods inside the paintComponent() method. I am using for loop to do that but not able to do it...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...
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,...

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.