1

I have a map mapping a character to a 2-d array of rules to process that character. Following is the code:

struct CFG //this struct is data structure to contain a context free grammar
{
   vector<char> *V;
   vector<char> *T;
   map<char,vector<vector<rhs>*>*> *prod;
  // char start;
};

int main()
{
map<char,vector<vector<rhs>*>*> *prod;  //rhs is the type of each cell of the 2-d array
CFG cfg1;
cfg1.V= new vector<char>;

//We imput elements into the V array here......//

cfg1.prod= new map<char,vector<vector<rhs>*>*>;

for(int i=0;i<cfg1.V->size();i++)
 {
    vector<vector<rhs>*>* all_prod_of_one_nonter= new vector<vector<rhs>*>;
    *(cfg1.prod)[*(cfg1.V)[i]]=all_prod_of_one_nonter;  //error occurs here//////
 }
}

In the line, I marked as 'error occurs', the following error occurs:

q1.cpp: In function ‘int main()’:
q1.cpp:93:29: error: no match for ‘operator*’ in ‘**(cfg1.CFG::V + ((unsigned int)(((unsigned int)i) * 12u)))’

I use * to dereference the pointer cfg1.V so that I can use subscript notation to access the array cells. How to remove the error?

8
  • 8
    This is extremely non-idiomatic C++. You should strongly consider not using a pointer to a map of pointers to vectors of pointers to vectors. Basically, just get rid of all the pointers entirely; you'll find the resulting code a lot simpler. Commented Mar 6, 2013 at 18:50
  • So many asterisk, aaargh. Commented Mar 6, 2013 at 18:52
  • You don't really need any of the pointers, they are all non-functional in your code. Start by removing all the * s and ->s and news. Commented Mar 6, 2013 at 18:54
  • Use a reference - Also you have memory leaks. Commented Mar 6, 2013 at 18:58
  • @OliCharlesworth...you are correct..But, I am in the process of learning about STL in c++ and trying to get comfortable in using them along with pointers.Hence the labyrinth.. Commented Mar 6, 2013 at 19:00

1 Answer 1

1
(*cfg1.prod)[(*cfg1.V)[i]]=all_prod_of_one_nonter;  

(Reason: operator[] (array subscripting) binds more tightly than operator* (indirection).)

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, I'm sure it did, but still, please consider Oli Charlesworth's advice in the comments to your question.
On second thought, it shouldn't have worked because the same problem persists within the array subscript. Edited answer to reflect this.
@us2012..inside I had changed the subscript and instead had used at function. So, it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.