Archived

This topic is now archived and is closed to further replies.

Another question, acessing functions

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve been haveing trouble accessing some functions, I want to use some that are not in the same file (I''m using visual c++ in a workspace). the function is declared in a header that both files use, and its not under anything, like a struct or class. The error I''m getting is that its a undeclared identifier. Any help would be apperciated. Thank you, One-Winged Angel

Share this post


Link to post
Share on other sites
Hmm, have you tried putting extern before it in the header? I''ve never had any problems with functions, but I had a similar problem with global variables and that fixed it.



-Deku-chan

DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)

Share this post


Link to post
Share on other sites
I think I might not have explained it right, the function I want to use is in one file and I want to use it in another, its just declared in the header. I put extern in front of it and it didn''t work, thx for the help anyways.

Share this post


Link to post
Share on other sites
If there is a header file with the function prototype in (eg. void MyFunction(int param1, int param2); ) then it should work fine. Check you didn''t make a mistake with the function prototype in the header.

By the way, I''m not sure if you''re making this mistake or not, but you shouldn''t define whole functions in header files, since that means they''ll get compiled once for each code file. You basically want functions defined in one single code file, and others use it through seeing the prototype in the header.

Share this post


Link to post
Share on other sites
I don''t know why I didn''t realize this before and mention it, the place where I''m trying to call the other function is from a function inside a struct, kinda like this:

fn I want
..
..
struct...
___fn im in...

I am very sorry about not explaining it right before, I''m kinda new to C++, if you couldn''t figure that out already.

Share this post


Link to post
Share on other sites
Try to write your prototype at the beginning of your file were you need it.

like:
// a.cpp
void Function(int a)
{
}

// b.cpp
void Function(int a);

...

Funtion( 0 );


If it''s work maybe your include is wrong or maybe your prototype is hibe by some #if #endif



Why English rules?? C pas très malin tout ça!

Share this post


Link to post
Share on other sites
well I tried what you guys said, and nothing works.
I evan tried coping the function i need into the same area. well if any of you that are not busy, and have vc++ can take a look at the code if you email me at demonicraptor@hotmail.com.
Thanks for trying to help!

Share this post


Link to post
Share on other sites
Ok, it sounds like you''re making a fundamental mistake then. Can you show us:

a) The line of code that calls the function.
b) The line of code that includes the header file for the function
c) The line of code in the header file that is the function prototype.
d) The exact error from MSVC when you try to compile it.

That should be all we need.

Share this post


Link to post
Share on other sites
call fn: loadmap("map0.mrpg");
this in in game.cpp

header file including: #include "thehead.h"
in the game.cpp and mastermain.cpp

what header file says:void loadmap(char* filename);
in the thehead.h

error: c:\my programs\other\game.cpp(189) : error C2065: ''laodmap'' : undeclared identifier

Just incase, here is the the fn I want to call:
// load up a map
void loadmap(char* filename)
{
ZeroMemory(&level,sizeof(level)); // clear level structure
ifstream fin(filename,ios::binary);
fin.read((char*)&level,sizeof(level));
fin.close();
}

this is in mastermain.cpp

Share this post


Link to post
Share on other sites
Did you actually copy and paste all of this: because I''m guessing you didn''t.

This is what you claim to have written:
call fn:   loadmap("map0.mrpg"); 


But this is the error that looks like it was copied from MSVC:
error:   c:\my programs\other\game.cpp(189) : error C2065: ''laodmap'' : undeclared identifier 


Notice the misspelling of ''loadmap'' as ''laodmap''. I bet you typed laodmap in game.cpp. Check it.

Share this post


Link to post
Share on other sites