Advertisement

function declarations

Started by March 27, 2001 02:19 PM
8 comments, last by omegasyphon 23 years, 10 months ago
why is it when i declare my function in header file like so it works fine void draw(); void draw() { cout<<"draw"<<endl; } when i create a source file for that header file and move the function code to it my program doesnt work. and i stil dont have a dam clue how to use extern since msvc help is of absolute no use.
And the error is?
Advertisement
Let me see if I understand. You should have the function declaration in a header file. For example
    // header.h#ifndef _MY_HEADER_#define _MY_HEADER_void draw();#endif  And you should have a cpp file like:      #include "header.h"void draw(){    // Your function definition here}    


Now for any other .cpp files in which you want to use the draw function you just include the header file ( header.h in this case ) as long as they are all in the same project.


-------
Andrew

Edited by - acraig on March 27, 2001 3:31:10 PM
well this is what i did but it still doesnt work

#ifndef __oglfunch__
#define __oglfunch__
void draw();
#endif

Need a little more information. Like what is the error that the compiler responds with. I don''t think that "doesn''t work" is a valid compiler error



-------
Andrew
MyHeader.h
  #ifndef INC_MY_HEADER#define INC_MY_HEADERextern void MyFunc(void);#endif  

MyHeader.cpp (Included in your project)
  #include <stdio.h>void MyFunc(void) {  printf("This is MyFunc.");}  

MyFileThatUsesMyFunc.cpp (Included in your project)
  #include "MyHeader.h"int main(void) {  MyFunc();  return 0;}  


"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://www.gdarchive.net/druidgames/
Advertisement
this is the linker error i get

maingl.obj : error LNK2001: unresolved external symbol "void __cdecl readmap(void)" (?readmap@@YAXXZ)

im trying to do the above what null and void has done

actually i think i figured out the problem.
does my header file need to be the same name as my the source file that the function is declared in? because in mine
the header file named gfunc.h
had the prototype to the function
and the source file named
glprog.cpp contained the source

Edited by - omegasyphon on March 27, 2001 8:25:51 PM
Probably you did not add MyHeader.cpp to the project. That would give you that error. There should be an ''add file to project'' or similar option somewhere- do that.

The Falling Frog
"The reasonable man adapts himself to the world, butthe unreasonable man tries to adapt the world to him-therefore, all progress depends on the unreasonable man." -Samuel Butler
now i get this new linker error

oglfunc.obj : error LNK2005: "int (* map)[10]" (?map@@3PAY09HA) already defined in glmain.obj
Debug/oglprog1.exe : fatal error LNK1169: one or more multiply defined symbols found

i have map defined in the oglfunc.h header file
like this

int map[10][10];

if i do this

extern int map[10][10]
i get the following linker errors

glmain.obj : error LNK2001: unresolved external symbol "int (* map)[10]" (?map@@3PAY09HA)
oglfunc.obj : error LNK2001: unresolved external symbol "int (* map)[10]" (?map@@3PAY09HA)

Edited by - omegasyphon on March 27, 2001 10:42:45 PM
Yes you must put
int map[10][10];
in your cpp-file.
The extern keyword only tells the compiler that the symbol exists, but it doesn''t define it, so the linker won''t find it, unless you do the above.
So you should put
extern int map[10][10];
in your header file, to tell all files that includes it that it exists.

This topic is closed to new replies.

Advertisement