942,790 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1445
  • C++ RSS
Dec 21st, 2009
0

undefined reference...?

Expand Post »
Had a moment to sit down and plow through a chapter, and as usual, wound up stuck. My compiler is giving me a string of undefined reference to SALES::[all my function names] in my main function. It's entirely possible that there are problems within the function definitions, but right now I can't even get the compiler to take a look at them, since my design is somehow failing to call them from main().
C++ Syntax (Toggle Plain Text)
  1. // exercise9.4 header file
  2.  
  3. #ifndef EXERCISE9_4_HEADER_H_INCLUDED
  4. #define EXERCISE9_4_HEADER_H_INCLUDED
  5.  
  6. namespace SALES
  7. {
  8. const int QUARTERS = 4;
  9. struct Sales
  10. {
  11. double sales[QUARTERS];
  12. double average;
  13. double max;
  14. double min;
  15. };
  16.  
  17. void setSales(Sales & s, const double ar[], int n); //non-interactive
  18.  
  19. void setSales(Sales & s); //interactive
  20.  
  21. void showSales(const Sales & s); //display
  22. }
  23. #endif // EXERCISE9_4_HEADER_H_INCLUDED
C++ Syntax (Toggle Plain Text)
  1. // Exercise 9.4 function definitions
  2. #include <iostream>
  3. #include "exercise9.4_header.h"
  4.  
  5. using namespace std;
  6. using namespace SALES;
  7.  
  8.  
  9. /* copies the lesser of 4 or n items from the array ar
  10.   to the sales member of s and computes and stores the
  11.   average, maximum, and minimum values of the entered items.
  12.   The remaining elements of sales, if any, get set to 0. */
  13. void setSales(Sales & s, const double ar[], int n)
  14. {
  15. //stuff, can post if necessary
  16. }
  17.  
  18. /* gathers sales for 4 quarters interactively, stores them
  19.   in the sales member of s, and computes and stores the
  20.   average, maximum, and minimum values */
  21. void setSales(Sales & s)
  22. {
  23. // more stuff
  24. }
  25.  
  26. // display all information in structure s
  27. void showSales(const Sales & s)
  28. {
  29. //display-ish stuff
  30. }
C++ Syntax (Toggle Plain Text)
  1. // Exercise 9.4 main function
  2. #include <iostream>
  3. #include "exercise9.4_header.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.  
  10. using namespace SALES;
  11. Sales test;
  12. for (int i = 0; i < QUARTERS; i++)
  13. test.sales[i] = 0;
  14. double testArrayA[QUARTERS] = {0.0, 33.3, 66.7, 100.0};
  15. double testArrayB[QUARTERS] = {25, 75, 0, 0};
  16. setSales(test);
  17. showSales(test);
  18. cout << "Test Array A:\n";
  19. setSales(test, testArrayA, 4);
  20. showSales(test);
  21. cout << "Test Array B:\n";
  22. setSales(test, testArrayB, 2);
  23. showSales(test);
  24.  
  25. return 0;
  26. }
I'm 9/10 sure I'm just doing something wrong in my main function, but I obviously can't see what it is. Help?
Last edited by grib; Dec 21st, 2009 at 7:37 pm. Reason: I think the function defs are irrelevant for the moment...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grib is offline Offline
15 posts
since Nov 2009
Dec 21st, 2009
1
Re: undefined reference...?
In your second listing (function definitions) the functions you define do not belong to any namespace. using namespace SALES; merely informs the compiler to decorate referred names. You should put them in a namespace the same way you did in the header file, that is inside namespace SALES {...}
Reputation Points: 707
Solved Threads: 185
Practically a Posting Shark
nezachem is offline Offline
843 posts
since Dec 2009
Dec 21st, 2009
0
Re: undefined reference...?
I am able to get it to compile, minus a total variable that appears out of nowhere and some cin's that had the << operator instead of the >> (I guess I caught your code before you changed it). It was balking about the signature of the setSales function not matching but that resolved. What is the error you are getting now? It could have to do with your namespaces and needing a using statement in main but I didn't test that.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Dec 21st, 2009
0
Re: undefined reference...?
Click to Expand / Collapse  Quote originally posted by nezachem ...
In your second listing (function definitions) the functions you define do not belong to any namespace. using namespace SALES; merely informs the compiler to decorate referred names. You should put them in a namespace the same way you did in the header file, that is inside namespace SALES {...}
OK, you're absolutely right, but after adding namespace SALES { around line 8 of the functions file (and a closing curly at the end), it's still giving me the same compile error -- undefined reference to foo.

Click to Expand / Collapse  Quote originally posted by jonsca ...
I am able to get it to compile, minus a total variable that appears out of nowhere and some cin's that had the << operator instead of the >> (I guess I caught your code before you changed it). It was balking about the signature of the setSales function not matching but that resolved. What is the error you are getting now? It could have to do with your namespaces and needing a using statement in main but I didn't test that.
The "total" was supposed to be "sum," oops. I changed it for no good reason midway -- bad Grib! I haven't even managed to get the functions to load into the compiler; it's getting hung up in main() and not even trying to compile the functions. Curremtly, that file looks like:
C++ Syntax (Toggle Plain Text)
  1. // Exercise 9.4 function definitions
  2. #include <iostream>
  3. #include "exercise9.4_header.h"
  4.  
  5. namespace SALES
  6. {
  7. /* copies the lesser of 4 or n items from the array ar
  8.   to the sales member of s and computes and stores the
  9.   average, maximum, and minimum values of the entered items.
  10.   The remaining elements of sales, if any, get set to 0. */
  11. void setSales(Sales & s, const double ar[], int n)
  12. {
  13. using namespace std;
  14. double sum;
  15. if (n = 0)
  16. {
  17. for (int i = 0; i < QUARTERS; i++)
  18. s.sales[i] = 0;
  19. s.min = 0;
  20. s.max = 0;
  21. s.average = 0;
  22. return;
  23. }
  24. s.min = ar[0];
  25. s.max = ar[0];
  26. for (int i = 0; i < n; i++)
  27. {
  28. s.sales[i] = ar[i];
  29. sum += ar[i];
  30. if (ar[i] < s.min)
  31. s.min = ar[i];
  32. else if (ar[i] > s.max)
  33. s.max = ar[i];
  34. }
  35. for (int i = n; i < QUARTERS; i++)
  36. {
  37. s.sales[i] = 0;
  38. }
  39. s.average = sum / n;
  40. return;
  41. }
  42.  
  43. /* gathers sales for 4 quarters interactively, stores them
  44.   in the sales member of s, and computes and stores the
  45.   average, maximum, and minimum values */
  46. void setSales(Sales & s)
  47. {
  48. using namespace std;
  49. double ar[QUARTERS];
  50. int n;
  51. cout << "\nHow many quarters will you input data in this year? _\b";
  52. while ((!(cin >> n)) && n >= 0 && n <= QUARTERS)
  53. {
  54. cin.clear();
  55. cin.ignore(1000, '\n');
  56. cout << "\nInput a number from 0 to " << QUARTERS << ", please: _\b";
  57. }
  58. for (int i = 0; i < n; i++)
  59. {
  60. cout << "\nInput sales for quarter #" << i << ": ";
  61. while (!(cin >> ar[i]))
  62. {
  63. cin.clear();
  64. cin.ignore(1000, '\n');
  65. cout << "\nInput a number, please. ";
  66. }
  67. }
  68. setSales(s, ar, n);
  69. return;
  70. }
  71.  
  72. // display all information in structure s
  73. void showSales(const Sales & s)
  74. {
  75. using std::cout;
  76. using std::endl;
  77. for (int i = 0; i < QUARTERS; i++)
  78. cout << "Quarter #" << i + 1 << ": $" << s.sales[i] << endl;
  79. cout << "Average quarterly sales: $" << s.average << endl;
  80. cout << "Worst quarter's sales: $" << s.min << endl;
  81. cout << "Best quarter's sales: $" << s.max << endl;
  82. return;
  83. }
  84. }
...and the errors I'm getting are, specifically:
C++ Syntax (Toggle Plain Text)
  1. obj\Debug\exercise9.4_main.o||In function `main':|
  2. |16|undefined reference to `SALES::setSales(SALES::Sales&)'|
  3. |17|undefined reference to `SALES::showSales(SALES::Sales const&)'|
  4. |19|undefined reference to `SALES::setSales(SALES::Sales&, double const*, int)'|
  5. |20|undefined reference to `SALES::showSales(SALES::Sales const&)'|
  6. |22|undefined reference to `SALES::setSales(SALES::Sales&, double const*, int)'|
  7. |23|undefined reference to `SALES::showSales(SALES::Sales const&)'|
  8. ||=== Build finished: 6 errors, 0 warnings ===|
  9.  
Last edited by grib; Dec 21st, 2009 at 8:23 pm. Reason: more stuff, and repost
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grib is offline Offline
15 posts
since Nov 2009
Dec 21st, 2009
0
Re: undefined reference...?
I just want to ask for comparison's sake, but what compiler are you using?
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Dec 21st, 2009
0
Re: undefined reference...?
I'm currently using Code::Blocks on Windows XP (too poor for new Windows, but that won't be too long in coming). Free, and brain-dead setup.
Last edited by grib; Dec 21st, 2009 at 8:24 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grib is offline Offline
15 posts
since Nov 2009
Dec 21st, 2009
1
Re: undefined reference...?
Try going to a prompt and compiling it as g++ exercise9.4.cpp exercise9.4main.cpp -o ex94 substitute your own filenames and exe file name. Somehow I think your project in C::B is not set up correctly (I don't use it so I don't know how to remedy it). You may be trying to compile your main.cpp first and it doesn't have the other code to link in.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Dec 21st, 2009
0
Re: undefined reference...?
Click to Expand / Collapse  Quote originally posted by jonsca ...
Try going to a prompt and compiling it as g++ exercise9.4.cpp exercise9.4main.cpp -o ex94 substitute your own filenames and exe file name. Somehow I think your project in C::B is not set up correctly (I don't use it so I don't know how to remedy it). You may be trying to compile your main.cpp first and it doesn't have the other code to link in.
Ah, that was it. Code::Blocks was, for some reason, not recognizing my functions file as actual code. Cut, delete, new project file, paste, ta-dah.

Now, my functions absolutely aren't doing what they ought to, but it's back into territory I know how to blunder my way through.

Thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grib is offline Offline
15 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: MP3 Library Program help!
Next Thread in C++ Forum Timeline: exit an infinite loop?





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


Follow us on Twitter


© 2011 DaniWeb® LLC