473,320 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 3584
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*(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
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...
3
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
by: Ike Naar | last post by:
Given the following C++ snippet: template < typename T > class A { // 1 public : // 2 class AA { } ; // 3 } ; ...
1
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...
4
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
by: Mark P | last post by:
Is this legal and sensible? class Outer { friend struct Inner { ... }; ...
9
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
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
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...
1
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.