?
Solved

Associative array of string

Posted on 2003-03-28
5
Medium Priority
?
240 Views
Last Modified: 2010-04-01
I need to create a associative array like Perl Hash. I tried with a operator [] overload, like this:

struct line {
        string option;
        string value;
        }

class ini
{    public:
           string operator[](string);
           { ....     }
 }

void main()
{
 line op[30];
 string op2;
 ...
 op2 = op["something"].value;
 ...
 }

And this causes the next error "invalid types `op[30][const char (*)[7]]' for array subscript".

Thanks for your help.

0
Comment
Question by:ragys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Expert Comment

by:captnoord
ID: 8224975
line op[30];   /// what do you mean with line
string op2;  //= logical
...
//op2 = op["something"].value;
op2 = op[number]; // I think

greets
captnoord

0
 

Expert Comment

by:captnoord
ID: 8225025
I think that line has to be CString
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 8225088
>>I need to create a associative array like Perl Hash

Use a STL map - that basically is the same. E.g.

#include <string>
#include <map>

using namespace std;

typedef map<string,string> line;

line op;

// add stuff

string op2 = op [ "something"];
0
 
LVL 12

Expert Comment

by:Salte
ID: 8225171
STL map - at least in the GNU version - isn't an array though but a red black tree :-)

In any case, jkr is right, just use std::map it is 'virtually' the same thing.

If you absolutely want an associative array it can only mean this is homework and in that case it is against EE policy to help you too much. However, we CAN help you with specific questions and so here goes:

struct line {
       string option;
       string value;
       }

Fair enough, it defines two strings, I assume one of them is the key and the other is the value of the associative array.

class ini
{    public:
          string operator[](string);
          { ....     }
}

This class has some problems, you define an operator [] that takes a string and return a string. I would very much like to see the body of that function as the class has no members. What array does that function attempt to access?
typically a class that defines an indexing operator must have some means of accessing a collection. I see no collection inside the class ini....

void main()
{
line op[30];

op is here a simple array, not an associative array.

string op2;
...
op2 = op["something"].value;

since op is a simple array you can't use a string to access it but must use a regular integer. The operator [] that you wrote was for class ini. I see no 'ini' around here, do you? Specifically op must be an instance of ini for the op["something"] to work and whatever that [] operator returns is the return value from op["something"]; Since that return type is string and not 'line' it doesn't make sense to write op["something"].value; For this to work op must be of class ini (which it isn't) and the operator [] in the class ini must have a return type of line or line reference (which it doesn't).

...
}

And this causes the next error "invalid types `op[30][const char (*)[7]]' for array subscript".


I hope the above explains why you get the error in your program.

Alf
0
 

Author Comment

by:ragys
ID: 8269058
Very thanks...
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question