Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C++ pointer help.

  • 05-03-2009 10:37pm
    #1
    Registered Users, Registered Users 2 Posts: 229 ✭✭


    Hey guys, I am trying to send a pointer to an object into a function. The function then uses the object as a key in a map. Can't really figure it out, this is what I have so far.
    //this is some of the main program so basically want to send current into memory. 
    Main.cpp    
        Brain memory;    // create memory.
        State current;    // stores values for current state.
        //set some values in current. 
        memory.remember(&current, 2);
    
    
    //Brain calls
    Brain.cpp
    void Brain::remember(State *val, int qvalue){
        cout << "storing to map" << endl;
        paths.insert ( pair<State, int>(*val, qvalue) );
    }
    //prototype.
    Brain.h
    private:
        map<State*, int> paths;        // map to hold memory
    public:
        void remember(State*, int);    // saves state, qvalue to map
    

    Here is my error
    /usr/include/c++/4.0.0/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = State, _U2 = int, _T1 = State* const, _T2 = int]’:
    brain.cpp:14: instantiated from here
    /usr/include/c++/4.0.0/bits/stl_pair.h:90: error: cannot convert ‘const State’ to ‘State* const’ in initialization
    (really ugly)

    I'm just doing something really stupid. Any help would be great..


Comments

  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    hmmm...

    The map key is declared as type pointer yet you trying to pass a value to it instead.

    What about:
    paths.insert ( pair<State*, int>(val, qvalue) );
    

    You have to be careful that the life of the State object is longer than that of the Brain object or you will get segmentation errors.


  • Registered Users, Registered Users 2 Posts: 229 ✭✭Katniss everMean


    Webmonkey <3 thank you :D I'm finding it hard to get my head around pointers.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Yeah they hard, but not as hard as people think.
    int i = 5;   // normal declaration of i
    int * j;  // create a pointer of type int
    
    j = &i; // assign the memory address of 'i' to j, so i and j now BOTH point to same data in memory
    
    printf("%d", *j), // print value of j (AND i), by dereferenceing j.
    
    *j = 3; // change value in memory to 3, now both i and j update to 3. But remember j  is a pointer so you must dereference it before seeing data or else you'll get a memory address printed.
    

    i is now 6.


  • Registered Users, Registered Users 2 Posts: 229 ✭✭Katniss everMean


    Thanks :D i'm sure at some stage it will just be naturally to do.


  • Closed Accounts Posts: 2,349 ✭✭✭nobodythere


    Pointers are pretty hard (glad I don't have them in the languages I use now! :)). Really you just have to keep practicing them, no amount of reading it will substitute for experience.

    I have a funny way I used to think about them, sorry if you know this, not sure if I can explain anyway

    I used to get these "invalid level of indirection" warnings from my compiler and copped it from that.

    Think of memory as a load of boxes where you can store information. Each box has a location (a memory address, looks something like 800012AC).

    Let's say we create an int called a, we have a "box" for it
    Also let's say we have a pointer, int *p.

    So:
       _____ Memory location of box is &a
      |     |
      |  a  |
      |_____|
         ^
         | *p
         |
       _____ Memory location of box is &p
      |     |
      |  p  |
      |_____|
    

    In the box p is a memory address, a 32-bit address.

    We can literally read *p as "the box pointed to by p", i.e. "the memory location pointed to by p".

    So if we have p in the box, *p is the data pointed to by the box. This is called "a level of indirection" but who in their right mind would call it that :p

    So if you think with this model, a line like "p = &a", becomes "put the memory address of 'a' in the box p".

    I use this paradigm because when you get to double pointers like **q and stuff you can apply it there too


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey




Advertisement