By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,812 Members | 856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,812 IT Pros & Developers. It's quick & easy.

friend und non-member function

P: n/a
hello,

i should implement this class:
Expand|Select|Wrap|Line Numbers
  1. namespace test_1
  2. {
  3. class statistician
  4. {
  5. public:
  6. // CONSTRUCTOR
  7. statistician( );
  8. // MODIFICATION MEMBER FUNCTIONS
  9. void next(double r);
  10. void reset( );
  11. // CONSTANT MEMBER FUNCTIONS
  12. int length( ) const;
  13. double sum( ) const;
  14. double mean( ) const;
  15. double minimum( ) const;
  16. double maximum( ) const;
  17. // FRIEND FUNCTIONS
  18. friend statistician operator +
  19. (const statistician & s1, const statistician & s2);
  20. friend statistician operator *
  21. (double scale, const statistician & s);
  22. private:
  23. int count;       // How many numbers in the sequence
  24. double total;    // The sum of all the numbers in the sequence
  25. double tinyest;  // The smallest number in the sequence
  26. double largest;  // The largest number in the sequence
  27. };
  28.  
  29. // NON-MEMBER functions for the statistician class
  30. bool operator ==(const statistician& s1, const statistician& s2);
  31. }
  32.  
But for me it is unfortuantely not clear what the actual difference
between the friend, non-member function (operator ==) and the normal
member function is and/or how these friend and non-member function have
to be definied in the implementation file...? :((

can anybody help me here?

matti

Sep 8 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
pat270881 wrote:
i should implement this class:
Expand|Select|Wrap|Line Numbers
  1. namespace test_1
  2. {
  3.    class statistician
  4.    {
  5.    public:
  6.        // CONSTRUCTOR
  7.        statistician( );
  8.        // MODIFICATION MEMBER FUNCTIONS
  9.        void next(double r);
  10.        void reset( );
  11.        // CONSTANT MEMBER FUNCTIONS
  12.        int length( ) const;
  13.        double sum( ) const;
  14.        double mean( ) const;
  15.        double minimum( ) const;
  16.        double maximum( ) const;
  17.        // FRIEND FUNCTIONS
  18.        friend statistician operator +
  19.            (const statistician & s1, const statistician & s2);
  20.        friend statistician operator *
  21.            (double scale, const statistician & s);
  22.    private:
  23.        int count;       // How many numbers in the sequence
  24.        double total;    // The sum of all the numbers in the sequence
  25.        double tinyest;  // The smallest number in the sequence
  26.        double largest;  // The largest number in the sequence
  27.    };
  28.    // NON-MEMBER functions for the statistician class
  29.    bool operator ==(const statistician& s1, const statistician& s2);
  30. }
  31.  

But for me it is unfortuantely not clear what the actual difference
between the friend, non-member function (operator ==) and the normal
member function is and/or how these friend and non-member function
have to be definied in the implementation file...? :((

can anybody help me here?
Open the namespace and implement them. Or prefix each name with the
name of the namespace. And if it's a member, prefix it with the name
of the class as well.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 8 '06 #2

P: n/a
pat270881 wrote:
hello,

i should implement this class:
Expand|Select|Wrap|Line Numbers
  1. namespace test_1
  2. {
  3.     class statistician
  4.     {
  5.     public:
  6.         // CONSTRUCTOR
  7.         statistician( );
  8.         // MODIFICATION MEMBER FUNCTIONS
  9.         void next(double r);
  10.         void reset( );
  11.         // CONSTANT MEMBER FUNCTIONS
  12.         int length( ) const;
  13.         double sum( ) const;
  14.         double mean( ) const;
  15.         double minimum( ) const;
  16.         double maximum( ) const;
  17.         // FRIEND FUNCTIONS
  18.         friend statistician operator +
  19.             (const statistician & s1, const statistician & s2);
  20.         friend statistician operator *
  21.             (double scale, const statistician & s);
  22.     private:
  23.         int count;       // How many numbers in the sequence
  24.         double total;    // The sum of all the numbers in the sequence
  25.         double tinyest;  // The smallest number in the sequence
  26.         double largest;  // The largest number in the sequence
  27.     };
  28.     // NON-MEMBER functions for the statistician class
  29.     bool operator ==(const statistician& s1, const statistician& s2);
  30. }
  31.  

But for me it is unfortuantely not clear what the actual difference
between the friend, non-member function (operator ==) and the normal
member function is and/or how these friend and non-member function have
to be definied in the implementation file...? :((

can anybody help me here?

matti
See these FAQs on friendship:

http://www.parashift.com/c++-faq-lite/friends.html

The gist is that the operator==() function can do all it needs without
access to the private parts (since it can use the public member
functions to retrieve and compare the data members), whereas the
operator+() function must modify the private data members of the
statistician object that it will return and cannot do so through the
public member functions. See also these guidelines about overloading
operators:

http://www.parashift.com/c++-faq-lit....html#faq-13.9

Cheers! --M

Sep 8 '06 #3

P: n/a

oh okay, thanks!! I have implemented only the skeleton of the
implementation class but when I run the testfile (stattest.cpp) some
strange errors occur:

My header-file
Expand|Select|Wrap|Line Numbers
  1. #ifndef STATS_H     // Prevent duplicate definition
  2. #define STATS_H
  3. #include <iostream>
  4.  
  5. namespace test_1
  6. {
  7. class statistician
  8. {
  9. public:
  10. // CONSTRUCTOR
  11. statistician( );
  12. // MODIFICATION MEMBER FUNCTIONS
  13. void next(double r);
  14. void reset( );
  15. // CONSTANT MEMBER FUNCTIONS
  16. int length( ) const;
  17. double sum( ) const;
  18. double mean( ) const;
  19. double minimum( ) const;
  20. double maximum( ) const;
  21. // FRIEND FUNCTIONS
  22. friend statistician operator +
  23. (const statistician & s1, const statistician & s2);
  24. friend statistician operator *
  25. (double scale, const statistician & s);
  26. private:
  27. int count;       // How many numbers in the sequence
  28. double total;    // The sum of all the numbers in the sequence
  29. double tinyest;  // The smallest number in the sequence
  30. double largest;  // The largest number in the sequence
  31. };
  32.  
  33. // NON-MEMBER functions for the statistician class
  34. bool operator ==(const statistician& s1, const statistician& s2);
  35. }
  36.  
  37. #endif
  38.  
The implementation-file, as said only the definition of the functions
in order to see if the testfile is started correctly at all:
Expand|Select|Wrap|Line Numbers
  1. #include "stats.h"
  2.  
  3. using namespace test_1;
  4.  
  5.  
  6. statistician::statistician()
  7. {
  8.  
  9. }
  10.  
  11. void statistician::next(double r)
  12. {
  13.  
  14. }
  15.  
  16. int statistician::length() const
  17. {
  18. return 0;
  19. }
  20.  
  21. double statistician::sum() const
  22. {
  23. return 0;
  24. }
  25.  
  26. double statistician::mean() const
  27. {
  28. return 0;
  29. }
  30.  
  31. double statistician::minimum() const
  32. {
  33. return 0;
  34. }
  35.  
  36. double statistician::maximum() const
  37. {
  38. return 0;
  39. }
  40.  
  41. bool operator ==(const statistician& s1, const statistician& s2)
  42. {
  43. return true;
  44. }
  45.  
  46. statistician operator + (const statistician & s1, const statistician &
  47. s2)
  48. {
  49. return s1;
  50. }
  51.  
  52. statistician operator * (double scale, const statistician & s)
  53. {
  54. return s;
  55. }
  56.  
The testfile:
Expand|Select|Wrap|Line Numbers
  1. #include <cctype   // Provides toupper
  2. #include <iomanip  // Provides setw to set the width of an output
  3. #include <iostream // Provides cout, cin
  4. #include <cstdlib  // Provides EXIT_SUCCESS
  5. #include "stats.h"
  6.  
  7.  
  8. using namespace main_savitch_2C;
  9. using namespace std;
  10.  
  11. // PROTOTYPES of functions used in this test program:
  12. void print_menu( );
  13.  
  14. char get_user_command( );
  15.  
  16. double get_number( );
  17.  
  18. void print_values(const statistician& s);
  19.  
  20. int main( )
  21. {
  22. statistician s1, s2, s3;  // Three statisticians for us to play
  23. with
  24. char choice;              // A command character entered by the
  25. user
  26. double x;                 // Value for multiplication x*s1
  27.  
  28. cout << "Three statisticians s1, s2, and s3 are ready to test." <<
  29. endl;
  30.  
  31. do
  32. {
  33. cout << endl;
  34. print_menu( );
  35. choice = toupper(get_user_command( ));
  36. switch (choice)
  37. {
  38. case 'R': cout << "Which one should I reset (1, 2, 3) " <<
  39. endl;
  40. choice = get_user_command( );
  41. switch (choice)
  42. {
  43. case '1': s1.reset( );
  44. break;
  45. case '2': s2.reset( );
  46. break;
  47. case '3': s3.reset( );
  48. break;
  49. }
  50. cout << "Reset activated for s" << choice << "."
  51. << endl;
  52. break;
  53. case '1': s1.next(get_number( ));
  54. break;
  55. case '2': s2.next(get_number( ));
  56. break;
  57. case '3': s3.next(get_number( ));
  58. break;
  59. case 'T': cout << "The values are given in this table:" <<
  60. endl;
  61. cout << "        LENGTH       SUM"
  62. << "   MINIMUM      MEAN   MAXIMUM" << endl;
  63. cout << "  s1";
  64. print_values(s1);
  65. cout << "  s2";
  66. print_values(s2);
  67. cout << "  s3";
  68. print_values(s3);
  69. break;
  70. case 'E': if (s1 == s2)
  71. cout << "s1 and s2 are equal." << endl;
  72. else
  73. cout << "s1 and s2 are not equal." << endl;
  74. break;
  75. case '+': s3 = s1 + s2;
  76. cout << "s3 has been set to s1 + s2" << endl;
  77. break;
  78. case '*': cout << "Please type a value for x: ";
  79. cin >x;
  80. s3 = x * s1;
  81. cout << "s3 has been set to " << x << " * s1" <<
  82. endl;
  83. break;
  84. case 'Q': cout << "Ridicule is the best test of truth." <<
  85. endl;
  86. break;
  87. default: cout << choice << " is invalid. Sorry." << endl;
  88. }
  89. }
  90. while ((choice != 'Q'));
  91.  
  92. return EXIT_SUCCESS;
  93.  
  94. }
  95.  
  96. void print_menu( )
  97. {
  98. cout << endl;
  99. cout << "The following choices are available: " << endl;
  100. cout << " R  Activate one of the reset( ) functions" << endl;
  101. cout << " 1  Add a new number to the 1st statistician s1" << endl;
  102. cout << " 2  Add a new number to the 2nd statistician s2" << endl;
  103. cout << " 3  Add a new number to the 3rd statistician s3" << endl;
  104. cout << " T  Print a table of values from the statisticians" <<
  105. endl;
  106. cout << " E  Test whether s1 == s2" << endl;
  107. cout << " +  Set the third statistician s3 equal to s1 + s2" <<
  108. endl;
  109. cout << " *  Set the third statistician s3 equal to x*s1" << endl;
  110. cout << " Q  Quit this test program" << endl;
  111. }
  112.  
  113. char get_user_command( )
  114. // Library facilties used: iostream.h
  115. {
  116. char command;
  117.  
  118. cout << "Enter choice: ";
  119. cin >command;
  120.  
  121. return command;
  122. }
  123.  
  124. double get_number( )
  125. // Library facilties used: iostream.h
  126. {
  127. double result;
  128.  
  129. cout << "Please enter the next real number for the sequence: ";
  130. cin  >result;
  131. cout << result << " has been read." << endl;
  132. return result;
  133. }
  134.  
  135. void print_values(const statistician& s)
  136. // Library facilties used: iostream.h
  137. {
  138. cout << setw(10) << s.length( );
  139. cout << setw(10) << s.sum( );
  140. if (s.length( ) != 0)
  141. {
  142. cout << setw(10) << s.minimum( );
  143. cout << setw(10) << s.mean( );
  144. cout << setw(10) << s.maximum( );
  145. }
  146. else
  147. cout << "      none      none      none";
  148. cout << endl;
  149. }
  150.  

The compilation works but when I run the testfile these errors occur:
Expand|Select|Wrap|Line Numbers
  1. ------ Build started: Project: Assignment1, Configuration: Debug Win32
  2. ------
  3. Compiling...
  4. stattest.cpp
  5. Linking...
  6. stattest.obj : error LNK2019: unresolved external symbol "class
  7. main_savitch_2C::statistician __cdecl
  8. main_savitch_2C::operator*(double,class main_savitch_2C::statistician
  9. const &)" (??Dmain_savitch_2C@@YA?AVstatistician@0@NABV10@@Z)
  10. referenced in function _main
  11. stattest.obj : error LNK2019: unresolved external symbol "class
  12. main_savitch_2C::statistician __cdecl main_savitch_2C::operator+(class
  13. main_savitch_2C::statistician const &,class
  14. main_savitch_2C::statistician const &)"
  15. (??Hmain_savitch_2C@@YA?AVstatistician@0@ABV10@0@Z) referenced in
  16. function _main
  17. stattest.obj : error LNK2019: unresolved external symbol "bool __cdecl
  18. main_savitch_2C::operator==(class main_savitch_2C::statistician const
  19. &,class main_savitch_2C::statistician const &)"
  20. (??8main_savitch_2C@@YA_NABVstatistician@0@0@Z) referenced in function
  21. _main
  22. stattest.obj : error LNK2019: unresolved external symbol "public: void
  23. __thiscall main_savitch_2C::statistician::reset(void)"
  24. (?reset@statistician@main_savitch_2C@@QAEXXZ) referenced in function
  25. _main
  26. C:\Dokumente und Einstellungen\matti\Eigene Dateien\Visual Studio
  27. 2005\Projects\Assignment1\Debug\Assignment1.exe : fatal error LNK1120:
  28. 4 unresolved externals
  29. Build log was saved at "file://c:\Dokumente und
  30. Einstellungen\matti\Eigene Dateien\Visual Studio
  31. 2005\Projects\Assignment1\Assignment1\Debug\BuildLog.htm"
  32. Assignment1 - 5 error(s), 0 warning(s)
  33.  
Does anybody know how i can fix that? :((

Sep 8 '06 #4

P: n/a


the namespace of the testfile is also test_1 and not main_savitch...,
that was a mistake in the posting...

Sep 8 '06 #5

P: n/a

I simply do not understand why these unresolved external symbol errors
occurs..??? :( Does nobody knows here how I can fix these errors?? :(

------ Build started: Project: Assignment1, Configuration: Debug Win32
------
Linking...
stattest.obj : error LNK2019: unresolved external symbol "class
main_savitch_2C::statistician __cdecl
main_savitch_2C::operator*(double,class main_savitch_2C::statistician
const &)" (??Dmain_savitch_2C@@YA?AVstatistician@0@NABV10@@Z )
referenced in function _main
stattest.obj : error LNK2019: unresolved external symbol "class
main_savitch_2C::statistician __cdecl main_savitch_2C::operator+(class
main_savitch_2C::statistician const &,class
main_savitch_2C::statistician const &)"
(??Hmain_savitch_2C@@YA?AVstatistician@0@ABV10@0@Z ) referenced in
function _main
stattest.obj : error LNK2019: unresolved external symbol "bool __cdecl
main_savitch_2C::operator==(class main_savitch_2C::statistician const
&,class main_savitch_2C::statistician const &)"
(??8main_savitch_2C@@YA_NABVstatistician@0@0@Z) referenced in function
_main
C:\Dokumente und Einstellungen\matti\Eigene Dateien\Visual Studio
2005\Projects\Assignment1\Debug\Assignment1.exe : fatal error LNK1120:
3 unresolved externals
Build log was saved at "file://c:\Dokumente und
Einstellungen\matti\Eigene Dateien\Visual Studio
2005\Projects\Assignment1\Assignment1\Debug\BuildL og.htm"
Assignment1 - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========

Sep 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.