944,001 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5785
  • C RSS
Aug 7th, 2005
0

Global Variables help

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster
Mahen is offline Offline
144 posts
since Aug 2004
Aug 7th, 2005
0

Re: Global Variables help

This has nothing to do with global variables, you simply never defined what mir is. (what data type)
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Aug 9th, 2005
0

Re: Global Variables help

And unless i'm mistaken, you forgot
  1. #include "test.h"
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Sauce is offline Offline
55 posts
since Jul 2005
Aug 9th, 2005
0

Re: Global Variables help

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 >>
Reputation Points: 10
Solved Threads: 2
Junior Poster
Mahen is offline Offline
144 posts
since Aug 2004
Aug 9th, 2005
0

Re: Global Variables help

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Sauce is offline Offline
55 posts
since Jul 2005
Aug 9th, 2005
0

Re: Global Variables help

Maybe something like: extern char mir[200]; in test.h can solve your problem ?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
freemind is offline Offline
62 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: problem again
Next Thread in C Forum Timeline: pls i need help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC