Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    May 2003
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    call-by-ref in function within function (C++)

    void somefunction(map<char,vector<node*> >&);
    void subfunction(map<char,vector<node*> >&, queue<int>&);

    void main(){
    map<char,vector<node*> > m1;
    somefunction(m1);
    }

    void somefunction(map<char,vector<node*> >& m){
    queue<int> Q;
    subfunction(m,Q);
    }

    void subfunction(map<char,vector<node*> >& m, queue<int>& q){
    //I = m.begin();
    node *ptr = I->second[q.front()];
    cout << ptr->data << endl;
    }

    The error is in the subfunction. Assuming the map is

    initialized with data, the "ptr->data" does not work. When I

    debug, it says ptr is a "Bad Ptr". Does this have anything to

    do with calling by reference within a function? It works fine when I replace "q.front()" with an integer. Thanks.

  • #2
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Some possible suggestions

    Your "I" is obviously meant to be an iterator, declare it as such. Use (*I).second instead of I->second. Then there is the apparent problem that Q will be empty in your code snippet, so q.front() will return an invalid value.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •