C++ help needed :(

The Teej

Also known as The Tjalian
OP
Former Staff
Joined
Jun 27, 2004
Messages
4,210
Trophies
0
Age
37
Location
England
Website
zeldapower.com
XP
643
Country
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?
 

The Teej

Also known as The Tjalian
OP
Former Staff
Joined
Jun 27, 2004
Messages
4,210
Trophies
0
Age
37
Location
England
Website
zeldapower.com
XP
643
Country
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?
 

CockroachMan

Scribbling around GBATemp's kitchen.
Member
Joined
Jan 14, 2006
Messages
3,887
Trophies
0
Age
38
Location
Brazil
Website
www.homembarata.com.br
XP
707
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
tongue.gif


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..
sleep.gif
 

The Teej

Also known as The Tjalian
OP
Former Staff
Joined
Jun 27, 2004
Messages
4,210
Trophies
0
Age
37
Location
England
Website
zeldapower.com
XP
643
Country
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
frown.gif
 

Issac

Iᔕᔕᗩᑕ
Supervisor
Joined
Apr 10, 2004
Messages
7,025
Trophies
3
Location
Sweden
XP
7,343
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)
 

Issac

Iᔕᔕᗩᑕ
Supervisor
Joined
Apr 10, 2004
Messages
7,025
Trophies
3
Location
Sweden
XP
7,343
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... :)
 

CockroachMan

Scribbling around GBATemp's kitchen.
Member
Joined
Jan 14, 2006
Messages
3,887
Trophies
0
Age
38
Location
Brazil
Website
www.homembarata.com.br
XP
707
Country
Brazil
The Teej said:
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
frown.gif


* is a pointer, so ** is a pointer to a pointer!
If you want to access the contents of a pointer, you also use *
tongue.gif

like:
Code:
int *a; // pointer to an integer

*a = 5;

printf("%d", a); //prints the address of a, something like 4244F123
printf("%d",*a); //prints 5

Now if you have a variable that's not a pointer and want to get it's address, you use &.

The code above can be writen as:
CODEint a; //integer variable

a = 5;

printf("%d",&a); //prints the address of a, something like 4244F123
printf("%d",a); //prints 5

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?
 

The Teej

Also known as The Tjalian
OP
Former Staff
Joined
Jun 27, 2004
Messages
4,210
Trophies
0
Age
37
Location
England
Website
zeldapower.com
XP
643
Country
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?
 

CockroachMan

Scribbling around GBATemp's kitchen.
Member
Joined
Jan 14, 2006
Messages
3,887
Trophies
0
Age
38
Location
Brazil
Website
www.homembarata.com.br
XP
707
Country
Brazil
The Teej said:
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?

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
wink2.gif
 

The Teej

Also known as The Tjalian
OP
Former Staff
Joined
Jun 27, 2004
Messages
4,210
Trophies
0
Age
37
Location
England
Website
zeldapower.com
XP
643
Country
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 *.
 

CockroachMan

Scribbling around GBATemp's kitchen.
Member
Joined
Jan 14, 2006
Messages
3,887
Trophies
0
Age
38
Location
Brazil
Website
www.homembarata.com.br
XP
707
Country
Brazil
The Teej said:
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 *.

CObject *m_pEnemies?
or
CObject *m_pEnemies[]?

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

CockroachMan

Scribbling around GBATemp's kitchen.
Member
Joined
Jan 14, 2006
Messages
3,887
Trophies
0
Age
38
Location
Brazil
Website
www.homembarata.com.br
XP
707
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
tongue.gif
 

The Teej

Also known as The Tjalian
OP
Former Staff
Joined
Jun 27, 2004
Messages
4,210
Trophies
0
Age
37
Location
England
Website
zeldapower.com
XP
643
Country
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 -.-
 

The Teej

Also known as The Tjalian
OP
Former Staff
Joined
Jun 27, 2004
Messages
4,210
Trophies
0
Age
37
Location
England
Website
zeldapower.com
XP
643
Country
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.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    SylverReZ @ SylverReZ: @Psionic Roshambo, Thats pretty cool.