Global Variables help

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2004
Posts: 140
Reputation: Mahen is an unknown quantity at this point 
Solved Threads: 2
Mahen Mahen is offline Offline
Junior Poster

Global Variables help

 
0
  #1
Aug 7th, 2005
Suppose i have a main.cpp file and a test.h file:
///////////////////////////////////////////
//main.cpp

#include <iostream.h>
#include <string.h>
void test();

char foo[200];
int main()
{
strcpy(foo, "testing");
test();
return 0;
}
//////////////////////////////////////////////////////////////
//test.h

void test()
{
cout << mir << endl;
}




WHY IS "mir" AN UNDECLARED IDENTIFIER
Want a new website for free:
http://indigo.kjots.com/
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Global Variables help

 
0
  #2
Aug 7th, 2005
This has nothing to do with global variables, you simply never defined what mir is. (what data type)
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 55
Reputation: Sauce is an unknown quantity at this point 
Solved Threads: 0
Sauce Sauce is offline Offline
Junior Poster in Training

Re: Global Variables help

 
0
  #3
Aug 9th, 2005
And unless i'm mistaken, you forgot
  1. #include "test.h"
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 140
Reputation: Mahen is an unknown quantity at this point 
Solved Threads: 2
Mahen Mahen is offline Offline
Junior Poster

Re: Global Variables help

 
0
  #4
Aug 9th, 2005
Am sorry but i left many mistakes, undertand me, i typed this quickly without passing tru a compiler. I WAS RUNNING OUT OF TIME
HERE IS THE CORRECTED CODE:

Suppose i have a main.cpp file and a test.h file:
  1. ///////////////////////////////////////////
  2. //main.cpp
  3.  
  4. #include <iostream.h>
  5. #include <string.h>
  6. #include "test.h"
  7.  
  8. void test();
  9.  
  10. char mir[200];
  11. int main()
  12. {
  13. strcpy(mir, "testing");
  14. test();
  15. return 0;
  16. }
  1. //////////////////////////////////////////////////////////////
  2. //test.h
  3.  
  4. void test()
  5. {
  6. cout << mir << endl;
  7. }
<< moderator edit: added [code][/code] tags >>
Want a new website for free:
http://indigo.kjots.com/
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 55
Reputation: Sauce is an unknown quantity at this point 
Solved Threads: 0
Sauce Sauce is offline Offline
Junior Poster in Training

Re: Global Variables help

 
0
  #5
Aug 9th, 2005
You are still not declaring mir in the test.h file. When you are using a variable in C and it is declared in a different file you have to declare it again using the "extern" keyword to let the compiler know that the variable is declared else where, so try...

main.c (using C code instead of C++)
  1. #include "test.h"
  2.  
  3. char mir[200]="testing";
  4.  
  5. int main()
  6. {
  7. test();
  8. return 0;
  9. }
test.h
  1. void test()
  2. {
  3. extern char mir[200];
  4. printf("%s\n, mir);
  5. }
  6.  

My compiler was complaining about my iostream.h file so i just used the printf instead and omitted the need for string.h but initializing the char array when i declared it.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 62
Reputation: freemind is an unknown quantity at this point 
Solved Threads: 1
freemind's Avatar
freemind freemind is offline Offline
Junior Poster in Training

Re: Global Variables help

 
0
  #6
Aug 9th, 2005
Maybe something like: extern char mir[200]; in test.h can solve your problem ?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4021 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC