473,562 Members | 2,587 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

friend und non-member function

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
5 3599
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
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

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


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

Sep 8 '06 #5

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*(dou ble,class main_savitch_2C ::statistician
const &)" (??Dmain_savitc h_2C@@YA?AVstat istician@0@NABV 10@@Z)
referenced in function _main
stattest.obj : error LNK2019: unresolved external symbol "class
main_savitch_2C ::statistician __cdecl main_savitch_2C ::operator+(cla ss
main_savitch_2C ::statistician const &,class
main_savitch_2C ::statistician const &)"
(??Hmain_savitc h_2C@@YA?AVstat istician@0@ABV1 0@0@Z) referenced in
function _main
stattest.obj : error LNK2019: unresolved external symbol "bool __cdecl
main_savitch_2C ::operator==(cl ass main_savitch_2C ::statistician const
&,class main_savitch_2C ::statistician const &)"
(??8main_savitc h_2C@@YA_NABVst atistician@0@0@ Z) referenced in function
_main
C:\Dokumente und Einstellungen\m atti\Eigene Dateien\Visual Studio
2005\Projects\A ssignment1\Debu g\Assignment1.e xe : fatal error LNK1120:
3 unresolved externals
Build log was saved at "file://c:\Dokumente und
Einstellungen\m atti\Eigene Dateien\Visual Studio
2005\Projects\A ssignment1\Assi gnment1\Debug\B uildLog.htm"
Assignment1 - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========

Sep 8 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1767
by: MstrControl | last post by:
Greetings All... I'm creating a BaseClass that will connect to a database and retrieve the all the properties values from it. So far so good. All the inherited classes retrieve it values from the database... IF the properties are PUBLIC. I'm using Reflection GetProperties and GetMemebers to retrieve the list of properties of each class.
3
2224
by: Mike - EMAIL IGNORED | last post by:
Following 14.5.3, I tried: class A { template<class T> friend class B; }; where A is a simple non-template class and B is a template. I tried to use a private method in B from a method in A.
3
2594
by: Ike Naar | last post by:
Given the following C++ snippet: template < typename T > class A { // 1 public : // 2 class AA { } ; // 3 } ; // 4 // 5 template < typename T > class B { // 6 friend class A < T > :: AA ; // 7 } ; // 8
1
2072
by: Chris Schadl | last post by:
Okay, I'm having a bit of a brain-fart and I can't remember how I would do this. Say I have the following: template <typename T1, typename T2> class A; // Forward declaration of A template <typename T1, typename T2> class B {
4
2161
by: ankit_jain_gzb | last post by:
Hi Iam not able to understand why the following code gives compile problem. Thanks Ankit Jain class B; class A{ public:
7
1937
by: Mark P | last post by:
Is this legal and sensible? class Outer { friend struct Inner { ... }; ...
9
1674
by: vineoff | last post by:
Why do I have to declare my oper<< as friend in my class as follows: class A { public: friend std::ostream& operator<<(std::ostream& out, const A& a) { return out << "foo"; } };
9
1662
by: p | last post by:
I know the word friend is not supported in c# and its not a very good thing to but i would be thankful if anybody can explain in simple words how can i implement it!
6
3154
by: WaterWalk | last post by:
I find friend declaration just very tricky. I tried the following examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are surprising. Example1: namespace ns1 { class Test { friend void func()
1
1452
by: Ralf Goertz | last post by:
Victor Bazarov wrote: Yes, you're right, they don't. At least not with my compiler. Is there a reason for this? I also tried to use a forward declaration of Derived2 before the definition of Derived1 but obviously, those declaration aren't allowed for templated classes, either. Again, I don't see why. If a non-templated class is used in a...
0
7658
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7630
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7940
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3609
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2075
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1192
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
907
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.