Skip to main content
gamedev.net
🔒 Locked

Quick Easy Pointer Question

Started by jrmiller84 10 replies 850+ views
Original Post
jrmiller84
jrmiller84
Hey guys, I'm working with linked lists for the 1st time and I think I'm pretty close to getting the animation system of my 2d scroller finished but am having a bit of trouble. The problem lies in the loop I made to find a specific item in the list. I need to test if the current item in the loop exists or not. I tried comparing it to NULL with an error "error C2065: 'NULL' : undeclared identifier." I also did some research and saw that someone recommended using Nullptr so I tried that and it didnt work as well, gave the same error. I understand the error and what it means just not sure of what to test it against to get what I'm looking for here. Here's my code, I'm sure this is a quick fix but I cant seem to find a good reference for the problem.

FRAME_MASTER CFrames::Retrieve(int Action) {

	FRAME_MASTER *Current = Last_Frame;

	int x = 0;

	for(x=0;(x<Frame_Directives && Current != NULL && Current->Action != Action); x++) {

		Current = Current->Next;

	}

	return *Current;

}


I will forever be a student.
TEUTON
TEUTON
There is no reason why NULL should not work...

Try only writing
(xAction != Action)
Nice Coder
Nice Coder
instead of (xAction != Action)

Try

((((xAction != Action))

hth
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
jrmiller84
jrmiller84
Yeah, I actually tried that first knowing that it should really be the same thing. When I do that, I don't get an error, it compiles and starts to run, then it gets an access violation error at runtime. Only thing I can think of is that maybe its going out of its bounds?
I will forever be a student.
jrmiller84
jrmiller84
It seems that just using Current as the arguement works. Now the access violation problem seems to be related to the last clause of that line of statements: Current->Action != Action. It seems I may not be linking the lists together correctly. This seems to be a pretty confusing concept.
I will forever be a student.
jrmiller84
jrmiller84
Wow, I realized what my problem was. I was redefining the previous frame structure everytime I added a new item onto the list. So it always had a null previous structure! I'm an idiot but... as I always say about programming when you figure out what you've done wrong it's a very rewarding activity. As soon as I fixed that error, it compiled and low and behold... had Megaman running on my screen. Thanks guys!
I will forever be a student.
Will F
Will F
If you're using C++, why not use std::list, rather than writing your own linked list? The C++ FAQ Lite has a section explaining why you should use the standard library's container classes. If you aren't comfortable using std::list here is a good Standard Library tutorial.

Here's a short code sample showing how to determine if something exists in a list

#include <list>#include <algorithm>#include <iostream>int main(){        std::list<int> foo;        foo.push_back(5);        std::list<int>::iterator itor;        itor = std::find(foo.begin(), foo.end(), 5);        if ( itor != foo.end() )                std::cout << "5 was found\n";        itor = std::find(foo.begin(), foo.end(), 7);        if ( itor == foo.end() )                std::cout << "7 was not found\n";}


It will output:
5 was found7 was not found

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.