943,865 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 535
  • C RSS
Aug 5th, 2009
0

Linking Error

Expand Post »
Compiling the following problem with visual studio 2000 gives the errors:

temp.obj : error LNK2001: unresolved external symbol "void __cdecl ungetch(int)" (?ungetch@@YAXH@Z)

temp.obj : error LNK2001: unresolved external symbol "int __cdecl getch(void)" (?getch@@YAHXZ)

Debug/temp.exe : fatal error LNK1120: 2 unresolved externals

Error executing link.exe.

The code is taken from Dennis Ritchie's "C Programming Language" 's page 97 (2nd Edition).
  1. #include<stdio.h>
  2. #include<ctype.h>
  3.  
  4. int getch(void);
  5. void ungetch(int);
  6.  
  7. int getint(int *pn)
  8. {
  9. int c, sign;
  10.  
  11. while (isspace(c=getch()));
  12.  
  13. if(!isdigit(c) && c != EOF && c != '+' && c != '-')
  14. {
  15. ungetch(c);
  16. return 0;
  17. }
  18.  
  19. sign = ( c == '-') ? -1 : 1;
  20.  
  21. if( c== '+' || c == '-')
  22. c = getch();
  23.  
  24. for( *pn = 0; isdigit(c); c = getch())
  25. *pn = 10 * *pn + (c - '0');
  26.  
  27. *pn *= sign;
  28.  
  29. if (c != EOF) ungetch(c);
  30.  
  31. return c;
  32. }
  33.  
  34. int main()
  35. {
  36. int *p;
  37.  
  38. p = new int[10];
  39.  
  40. p[0] = 4;
  41. p[1] = 2;
  42. p[2] = 6;
  43. p[3] = 8;
  44. p[4] = 2;
  45. p[5] = 1;
  46. p[6] = 7;
  47. p[7] = 1;
  48. p[8] = 2;
  49. p[9] = 1;
  50.  
  51. int i = getint(p);
  52.  
  53. printf("The value is %d", i);
  54.  
  55. return 0;
  56. }
Similar Threads
Reputation Points: 10
Solved Threads: 2
Newbie Poster
farhan.foxtrot is offline Offline
21 posts
since Aug 2008
Aug 5th, 2009
1

Re: Linking Error

getch() is a c language thing:
http://www.daniweb.com/forums/thread11811.html

I think that book was wanting you to include conio.h, but as you will see if you are working in c++ you likely shouldn't be using that.
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Aug 5th, 2009
0

Re: Linking Error

Thank you ,daviddoria , But can you give me a hint 'bout how to run the program .... I mean how to pass the parameter to the function?? I run the program and whatever I do I get the out put of

The value is 13
Reputation Points: 10
Solved Threads: 2
Newbie Poster
farhan.foxtrot is offline Offline
21 posts
since Aug 2008
Aug 5th, 2009
0

Re: Linking Error

main() is passing an array of ints to that function, but the function is using it like its a pointer to a single int.

>>The code is taken from Dennis Ritchie's "C Programming Language" 's page 97 (2nd Edition).
I doubt it -- he is a hell of a lot better programmer than that!
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Aug 6th, 2009
0

Re: Linking Error

Compiling the following problem with visual studio 2000 gives the errors:

temp.obj : error LNK2001: unresolved external symbol "void __cdecl ungetch(int)" (?ungetch@@YAXH@Z)

temp.obj : error LNK2001: unresolved external symbol "int __cdecl getch(void)" (?getch@@YAHXZ)

Debug/temp.exe : fatal error LNK1120: 2 unresolved externals

Error executing link.exe.

The code is taken from Dennis Ritchie's "C Programming Language" 's page 97 (2nd Edition).
  1. #include<stdio.h>
  2. #include<ctype.h>
  3.  
  4. int getch(void);
  5. void ungetch(int);
  6.  
  7. int getint(int *pn)
  8. {
  9. int c, sign;
  10.  
  11. while (isspace(c=getch()));
  12.  
  13. if(!isdigit(c) && c != EOF && c != '+' && c != '-')
  14. {
  15. ungetch(c);
  16. return 0;
  17. }
  18.  
  19. sign = ( c == '-') ? -1 : 1;
  20.  
  21. if( c== '+' || c == '-')
  22. c = getch();
  23.  
  24. for( *pn = 0; isdigit(c); c = getch())
  25. *pn = 10 * *pn + (c - '0');
  26.  
  27. *pn *= sign;
  28.  
  29. if (c != EOF) ungetch(c);
  30.  
  31. return c;
  32. }
  33.  
  34. int main()
  35. {
  36. int *p;
  37.  
  38. p = new int[10];
  39.  
  40. p[0] = 4;
  41. p[1] = 2;
  42. p[2] = 6;
  43. p[3] = 8;
  44. p[4] = 2;
  45. p[5] = 1;
  46. p[6] = 7;
  47. p[7] = 1;
  48. p[8] = 2;
  49. p[9] = 1;
  50.  
  51. int i = getint(p);
  52.  
  53. printf("The value is %d", i);
  54.  
  55. return 0;
  56. }
1. The problem is that you have declared getch and ungetch() but not defined it anywhere, so define it also.
2. You are mixing C++ with C, I think new opeartor is used with C++ but not with C.
Reputation Points: 45
Solved Threads: 7
Light Poster
9868 is offline Offline
36 posts
since Mar 2009

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: need help
Next Thread in C Forum Timeline: Bison ! "start symbol 'grammar' does not derive any sentence" meaning?





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


Follow us on Twitter


© 2011 DaniWeb® LLC