Linking Error

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

Join Date: Aug 2008
Posts: 21
Reputation: farhan.foxtrot is an unknown quantity at this point 
Solved Threads: 2
farhan.foxtrot farhan.foxtrot is offline Offline
Newbie Poster

Linking Error

 
0
  #1
Aug 5th, 2009
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. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Linking Error

 
1
  #2
Aug 5th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 21
Reputation: farhan.foxtrot is an unknown quantity at this point 
Solved Threads: 2
farhan.foxtrot farhan.foxtrot is offline Offline
Newbie Poster

Re: Linking Error

 
0
  #3
Aug 5th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,413
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1470
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Linking Error

 
0
  #4
Aug 5th, 2009
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!
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 36
Reputation: 9868 is on a distinguished road 
Solved Threads: 7
9868 9868 is offline Offline
Light Poster

Re: Linking Error

 
0
  #5
Aug 6th, 2009
Originally Posted by farhan.foxtrot View 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. }
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.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC