• 9
• 10
• 9
• 10
• 10

# Passing a pointer as a function argument

This topic is 4911 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey all. I'm posting this here because I am totally new to C++, and some of the concepts it involves. Pointers would be one of them. I've been looking at the pointer tutorial at cplusplus.com, but I cannot for the life of me figure out what I am doing wrong in trying to pass a pointer as an argument. Perhaps I don't even need to use a pointer, but it just seemed approporiate to pass by reference. Maybe I'm wrong. While it would be technically accurate to say that this is part of a homework question, and I'm fully aware of the GDNet policy on such, the assaignment is to model one of the colleges computing labs in OpenGL. To do this I decided to declare a number of structures that would keep account of the positions of the models, and use arrays to organise an finite number of these, as none would need to be created in runtime.
//declared in the global declarations area
typedef struct Computer{
int x,y; //coordinates relative to the room
}Computer;

int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Computer * c = &computers[0];
drawComputer(c);

return TRUE;
}

BOOL drawComputer(Computer * c)
{
//OpenGL co-ordinate code
return true;
}


I get these errors error C3861: 'drawComputer': identifier not found, even with argument-dependent lookup error C2365: 'drawComputer' : redefinition; previous definition was a 'formerly unknown identifier' To be totally honest, I've really only started working with VS about a week ago, so I still don't know exactly what she means by some things... But from I gather from this its can't seem to determine what I want to pass a pointer as the argument. I've also used
BOOL drawComputer(Computer *c)
and
BOOL drawComputer(Computer (*c))
, but I keep getting the same error. Anyone care to shed some light on the matter for me, please?

##### Share on other sites
bool drawComputer(Computer *c) is fine, and that should work. The error looks like it can't find the function. Make sure that either main comes after drawComputer, or above main you put the following:

bool drawComputer(Computer *c);

What I usually do is put all the prototypes at the top, and put the actual functions below main.

Other than that, your pointers look good.

##### Share on other sites
Your problem is pretty simple, its just that in DrawGLScene() the drawComputer() function doesn't exist yet. If you've programed in java or the like then the compiler is smart enough to find all classes and functions no matter where they are declared. c/c++ is different as it does it in order - so when the compiler gets to DrawGLScene() it hasn't yet found drawComputer() so can't figure out what todo.

To fix this problem you have to put a function definition at the top of your source file so that the compiler will know about the drawComputer() function when it gets to DrawGLScene(). A definition is just the function header with no {} ... like so:

BOOL drawComputer(Computer * c);

Then the compiler will know about drawComputer when it gets to DrawGLScene(), but the drawComputer() function's implimentation is still after DrawGLScene(). You'll get used to it :P

Also look up how to use header files on google oneday - they will also fix this problem when used properly.

##### Share on other sites
Oh thats it... drawComputer is declared after main(). Totally forgot about the whole predeclaration thing in C++. [rolleyes]

Been working in Java up until now, not a single tutorial in C++ before we dived into the OpenGL, and we have to do it in C++ because its the only language the lecturer is comfortable with!

Thanks!

##### Share on other sites
Quote:
 Original post by doodle_sketchOh thats it... drawComputer is declared after main()

Its not that its declared after main() - its that its decalred after its used. You must declared everything befor it gets used, so if its used in main() then sure you must declare it befor main but in this example your useing drawComputer befor DrawGLScene, so it has nothing todo with main, all you need is to declare drawComputer befor DrawGLScene!