function declarations
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.
Let me see if I understand. You should have the function declaration in a header file. For example
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
|
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
#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 ![](smile.gif)
-------
Andrew
![](smile.gif)
-------
Andrew
MyHeader.h
MyHeader.cpp (Included in your project)
MyFileThatUsesMyFunc.cpp (Included in your project)
"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.crosswinds.net/~druidgames/resist.jpg)
http://www.gdarchive.net/druidgames/
|
MyHeader.cpp (Included in your project)
|
MyFileThatUsesMyFunc.cpp (Included in your project)
|
"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.crosswinds.net/~druidgames/resist.jpg)
http://www.gdarchive.net/druidgames/
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
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 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
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.
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
Popular Topics
Advertisement
Recommended Tutorials
Advertisement