C++ help needed :(

Discussion in 'Computer Programming, Emulation, and Game Modding' started by The Teej, Apr 14, 2008.

Apr 14, 2008

C++ help needed :( by The Teej at 5:08 PM (1,805 Views / 0 Likes) 15 replies

  1. The Teej
    OP

    Former Staff The Teej Also known as The Tjalian

    Joined:
    Jun 27, 2004
    Messages:
    4,210
    Location:
    England
    Country:
    United Kingdom
    Hey all, had this issue for a little while now and I can't seem to fix it. Basically, in a class called Level, I have a get method which returns a particular pointer in a pointer array (the particular pointer is defined by a number that's passed when the method is called). I have another pointer array in another class called Game, and I use a for loop to replicate the pointer array in the Level Class (by using the accessor method).

    Now, the problem I'm having is that when I try to call upon the update method on one of the pointers (it's a pointer to a class) a run time error appears saying there is an unhandled exception coming from the pointer array in the game class. I check to see if the pointer isn't null first, and it's passing that check, so assumably there's a pointer there.
    Any ideas?
     
  2. The Teej
    OP

    Former Staff The Teej Also known as The Tjalian

    Joined:
    Jun 27, 2004
    Messages:
    4,210
    Location:
    England
    Country:
    United Kingdom
    Hmm, I think I know the issue - it's not cloning the pointer properly. I've created a new method to try and handle the cloning on the Level Class side:

    Level.cpp
    Code:
    //clone enemy pointer
    void
    CLevel::CloneLevelEnemyPointer(int iArrayPos_, CObject * pPassedEnemyPointer_)
    {
    ÂÂÂÂpPassedEnemyPointer_ = m_pEnemies[iArrayPos_];
    }
    and then basically pass the pointer I want to be used for the cloning:

    Game.cpp
    CODEÂÂÂÂ//pass enemy pointers from Level class
    ÂÂÂÂfor (int i = 0; i < MAXENEMIES; i++)
    ÂÂÂÂ{
    ÂÂÂÂÂÂÂÂm_pLevel->CloneLevelEnemyPointer(i, m_pLevelEnemies);
    ÂÂÂÂ}

    But I still get the same error. Is there a different method to cloning pointer arrays across classes which I am not aware of?
     
  3. CockroachMan

    Member CockroachMan Scribbling around GBATemp's kitchen.

    Joined:
    Jan 14, 2006
    Messages:
    3,889
    Location:
    Brazil
    Country:
    Brazil
    Your cloning function is wrong.. you need to pass a pointer to the pointer!

    Like this:
    Code:
    //clone enemy pointer
    void
    CLevel::CloneLevelEnemyPointer(int iArrayPos_, CObject ** pPassedEnemyPointer_)
    {
    ÂÂÂÂ*pPassedEnemyPointer_ = m_pEnemies[iArrayPos_];
    }
    CODEÂÂÂÂ//pass enemy pointers from Level class
    ÂÂÂÂfor (int i = 0; i < MAXENEMIES; i++)
    ÂÂÂÂ{
    ÂÂÂÂÂÂÂÂm_pLevel->CloneLevelEnemyPointer(i, &m_pLevelEnemies );
    ÂÂÂÂ}

    That should do the trick [​IMG]

    When a function is executed, a local copy of the arguments are created, so the way you were doing it, you were copying the pointer to a local variable.. it's complicated.. [​IMG]
     
  4. The Teej
    OP

    Former Staff The Teej Also known as The Tjalian

    Joined:
    Jun 27, 2004
    Messages:
    4,210
    Location:
    England
    Country:
    United Kingdom
    Thanks! There are a couple of questions, though..

    What's the difference between ** and *, and what's up with the & in front of the pointer? I'm still a C++ noob really, so a lot of the syntax is pretty alien to me, hehe.

    edit: also, what's with the * infront of the pPassedEnemyPointer pointer?

    EDIT: I'm still getting an access violation [​IMG]
     
  5. Issac

    Member Issac Mini-mod

    Joined:
    Apr 10, 2004
    Messages:
    4,465
    Location:
    Sweden
    Country:
    Sweden
    how about changing * for a &?

    I don't know though... just remember something with calling functions with &'s as parameters....
    (doin some c++ lab assignments right now)
     
  6. The Teej
    OP

    Former Staff The Teej Also known as The Tjalian

    Joined:
    Jun 27, 2004
    Messages:
    4,210
    Location:
    England
    Country:
    United Kingdom
    Change it where, though?
     
  7. Issac

    Member Issac Mini-mod

    Joined:
    Apr 10, 2004
    Messages:
    4,465
    Location:
    Sweden
    Country:
    Sweden
    CLevel::CloneLevelEnemyPointer(int iArrayPos_, CObject& pPassedEnemyPointer_)

    Edit: Though.. I'm completely unsure about that :S

    Edit2:
    an example in the lab assignment i'm doing:
    Code:
    void swap(Diver_info& a, Diver_info& b);
    ...
    ...
    void swap(Diver_info& a, Diver_info& b){
    ÂÂÂÂ Diver_info temp;
    ÂÂÂÂ temp = a;
    ÂÂÂÂ a = b;
    ÂÂÂÂ b = temp;
    }
    
    and with this... the struct Diver_info's a and b objects actually gets changed without returning anything.. much like pointers... :)
     
  8. CockroachMan

    Member CockroachMan Scribbling around GBATemp's kitchen.

    Joined:
    Jan 14, 2006
    Messages:
    3,889
    Location:
    Brazil
    Country:
    Brazil
    I think that the & on functions is basically the same thing as using *, not sure now, it works only on C++.. I'm more used to regular old C :/

    Have you tried debugging your code to see where is it crashing?
     
  9. The Teej
    OP

    Former Staff The Teej Also known as The Tjalian

    Joined:
    Jun 27, 2004
    Messages:
    4,210
    Location:
    England
    Country:
    United Kingdom
    Hold on, in that case, wouldn't

    Code:
    CLevel::CloneLevelEnemyPointer(int iArrayPos_, CObject ** pPassedEnemyPointer_)
    {
    ÂÂÂÂ*pPassedEnemyPointer_ = m_pEnemies[iArrayPos_];
    }
    actually make pPassedEnemyPointer the address of m_pEnemies[iArrayPos_], and not the actual pointer?
     
  10. CockroachMan

    Member CockroachMan Scribbling around GBATemp's kitchen.

    Joined:
    Jan 14, 2006
    Messages:
    3,889
    Location:
    Brazil
    Country:
    Brazil
    Nope, that would be &m_pEnemies[iArrayPos_].. unless the m_pEnemies is wrong.. what is the type of it?

    Pointers are very confuse to understand at first.. try making a simple program, make some weird pointers, try switching their values and see if the results are what you were expecting.. that should help you understand them better [​IMG]
     
  11. The Teej
    OP

    Former Staff The Teej Also known as The Tjalian

    Joined:
    Jun 27, 2004
    Messages:
    4,210
    Location:
    England
    Country:
    United Kingdom
    This isn't my first time with pointers, I've used static and dynamic pointers many times, I've just never attempted to use them across classes before.

    m_pEnemies is type CObject *.
     
  12. CockroachMan

    Member CockroachMan Scribbling around GBATemp's kitchen.

    Joined:
    Jan 14, 2006
    Messages:
    3,889
    Location:
    Brazil
    Country:
    Brazil
    CObject *m_pEnemies?
    or
    CObject *m_pEnemies[]?

    The first one is just an array of CObjects.. not pointers.
     
  13. The Teej
    OP

    Former Staff The Teej Also known as The Tjalian

    Joined:
    Jun 27, 2004
    Messages:
    4,210
    Location:
    England
    Country:
    United Kingdom
    It's the first one, as it's storing objects, not pointers.
     
  14. CockroachMan

    Member CockroachMan Scribbling around GBATemp's kitchen.

    Joined:
    Jan 14, 2006
    Messages:
    3,889
    Location:
    Brazil
    Country:
    Brazil
    Sorry.. made some tests using classes now..

    This should work:
    Code:
    CLevel::CloneLevelEnemyPointer(int iArrayPos_, CObject * pPassedEnemyPointer_)
    {
    ÂÂÂÂ*pPassedEnemyPointer_ = m_pEnemies[iArrayPos_];
    }
    ...
    m_pLevel->CloneLevelEnemyPointer(i, &m_pLevelEnemies[i] );
    ...
    You could also use:
    Code:
    CLevel::CloneLevelEnemyPointer(int iArrayPos_, CObjectÂÂ&pPassedEnemyPointer_)
    {
    ÂÂÂÂpPassedEnemyPointer_ = m_pEnemies[iArrayPos_];
    }
    ...
    m_pLevel->CloneLevelEnemyPointer(i, m_pLevelEnemies[i] );
    ...
    Mixing pointers and classes is confuse.. since you're using C++, you should try using STL classes like Vector and List instead of using pointers [​IMG]
     
  15. The Teej
    OP

    Former Staff The Teej Also known as The Tjalian

    Joined:
    Jun 27, 2004
    Messages:
    4,210
    Location:
    England
    Country:
    United Kingdom
    Are you sure that works? I get these errors:

    Level.cpp
    error C2679: binary '=' : no operator found which takes a right-hand operand of type 'CObject *' (or there is no acceptable conversion)

    Game.cpp
    error C2664: 'CLevel::CloneLevelEnemyPointer' : cannot convert parameter 2 from 'CObject **__w64 ' to 'CObject *'

    And, we haven't been taught Vector and List in class, so I have no idea how to use them. Guh, so much for my brilliant idea of a decent level engine, I can't figure out some trivial pointer issue -.-
     
  16. The Teej
    OP

    Former Staff The Teej Also known as The Tjalian

    Joined:
    Jun 27, 2004
    Messages:
    4,210
    Location:
    England
    Country:
    United Kingdom
    Hurr hurr hurr.

    Your original suggestion, Cockroach man, was 100% right. The issue was... that I wasn't calling the methods in the constructor! Also, I wasn't initialising my pointers to 0. So, I was always cloning garbage pointers!

    And thus, the problem is SOLVED.
     

Share This Page