friend und non-member function

P

pat270881

hello,

i should implement this class:
Code:
namespace test_1
{
    class statistician
    {
    public:
        // CONSTRUCTOR
        statistician( );
        // MODIFICATION MEMBER FUNCTIONS
        void next(double r);
        void reset( );
        // CONSTANT MEMBER FUNCTIONS
        int length( ) const;
        double sum( ) const;
        double mean( ) const;
        double minimum( ) const;
        double maximum( ) const;
        // FRIEND FUNCTIONS
        friend statistician operator +
            (const statistician & s1, const statistician & s2);
        friend statistician operator *
            (double scale, const statistician & s);
    private:
        int count;       // How many numbers in the sequence
        double total;    // The sum of all the numbers in the sequence
        double tinyest;  // The smallest number in the sequence
        double largest;  // The largest number in the sequence
    };

    // NON-MEMBER functions for the statistician class
    bool operator ==(const statistician& s1, const statistician& s2);
}

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
 
V

Victor Bazarov

pat270881 said:
i should implement this class:
Code:
namespace test_1
{
class statistician
{
public:
// CONSTRUCTOR
statistician( );
// MODIFICATION MEMBER FUNCTIONS
void next(double r);
void reset( );
// CONSTANT MEMBER FUNCTIONS
int length( ) const;
double sum( ) const;
double mean( ) const;
double minimum( ) const;
double maximum( ) const;
// FRIEND FUNCTIONS
friend statistician operator +
(const statistician & s1, const statistician & s2);
friend statistician operator *
(double scale, const statistician & s);
private:
int count;       // How many numbers in the sequence
double total;    // The sum of all the numbers in the sequence
double tinyest;  // The smallest number in the sequence
double largest;  // The largest number in the sequence
};

// NON-MEMBER functions for the statistician class
bool operator ==(const statistician& s1, const statistician& s2);
}

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
 
M

mlimber

pat270881 said:
hello,

i should implement this class:
Code:
namespace test_1
{
class statistician
{
public:
// CONSTRUCTOR
statistician( );
// MODIFICATION MEMBER FUNCTIONS
void next(double r);
void reset( );
// CONSTANT MEMBER FUNCTIONS
int length( ) const;
double sum( ) const;
double mean( ) const;
double minimum( ) const;
double maximum( ) const;
// FRIEND FUNCTIONS
friend statistician operator +
(const statistician & s1, const statistician & s2);
friend statistician operator *
(double scale, const statistician & s);
private:
int count;       // How many numbers in the sequence
double total;    // The sum of all the numbers in the sequence
double tinyest;  // The smallest number in the sequence
double largest;  // The largest number in the sequence
};

// NON-MEMBER functions for the statistician class
bool operator ==(const statistician& s1, const statistician& s2);
}

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-lite/operator-overloading.html#faq-13.9

Cheers! --M
 
P

pat270881

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
Code:
#ifndef STATS_H     // Prevent duplicate definition
#define STATS_H
#include <iostream>

namespace test_1
{
    class statistician
    {
    public:
        // CONSTRUCTOR
        statistician( );
        // MODIFICATION MEMBER FUNCTIONS
        void next(double r);
        void reset( );
        // CONSTANT MEMBER FUNCTIONS
        int length( ) const;
        double sum( ) const;
        double mean( ) const;
        double minimum( ) const;
        double maximum( ) const;
        // FRIEND FUNCTIONS
        friend statistician operator +
            (const statistician & s1, const statistician & s2);
        friend statistician operator *
            (double scale, const statistician & s);
    private:
        int count;       // How many numbers in the sequence
        double total;    // The sum of all the numbers in the sequence
        double tinyest;  // The smallest number in the sequence
        double largest;  // The largest number in the sequence
    };

    // NON-MEMBER functions for the statistician class
    bool operator ==(const statistician& s1, const statistician& s2);
}

#endif

The implementation-file, as said only the definition of the functions
in order to see if the testfile is started correctly at all:
Code:
#include "stats.h"

using namespace test_1;


statistician::statistician()
{

}

void statistician::next(double r)
{

}

int statistician::length() const
{
    return 0;
}

double statistician::sum() const
{
    return 0;
}

double statistician::mean() const
{
    return 0;
}

double statistician::minimum() const
{
    return 0;
}

double statistician::maximum() const
{
    return 0;
}

bool operator ==(const statistician& s1, const statistician& s2)
{
	return true;
}

statistician operator + (const statistician & s1, const statistician &
s2)
{
	return s1;
}

statistician operator * (double scale, const statistician & s)
{
	return s;
}

The testfile:
Code:
#include <cctype>    // Provides toupper
#include <iomanip>   // Provides setw to set the width of an output
#include <iostream>  // Provides cout, cin
#include <cstdlib>   // Provides EXIT_SUCCESS
#include "stats.h"


using namespace main_savitch_2C;
using namespace std;

// PROTOTYPES of functions used in this test program:
void print_menu( );

char get_user_command( );

double get_number( );

void print_values(const statistician& s);

int main( )
{
    statistician s1, s2, s3;  // Three statisticians for us to play
with
    char choice;              // A command character entered by the
user
    double x;                 // Value for multiplication x*s1

    cout << "Three statisticians s1, s2, and s3 are ready to test." <<
endl;

    do
    {
        cout << endl;
        print_menu( );
        choice = toupper(get_user_command( ));
        switch (choice)
        {
            case 'R': cout << "Which one should I reset (1, 2, 3) " <<
endl;
                      choice = get_user_command( );
                      switch (choice)
                      {
			  case '1': s1.reset( );
                                    break;
			  case '2': s2.reset( );
                                    break;
			  case '3': s3.reset( );
                                    break;
                      }
                      cout << "Reset activated for s" << choice << "."
<< endl;
                      break;
            case '1': s1.next(get_number( ));
                      break;
            case '2': s2.next(get_number( ));
                      break;
            case '3': s3.next(get_number( ));
                      break;
            case 'T': cout << "The values are given in this table:" <<
endl;
                      cout << "        LENGTH       SUM"
                           << "   MINIMUM      MEAN   MAXIMUM" << endl;
                      cout << "  s1";
                      print_values(s1);
                      cout << "  s2";
                      print_values(s2);
                      cout << "  s3";
                      print_values(s3);
                      break;
            case 'E': if (s1 == s2)
                          cout << "s1 and s2 are equal." << endl;
                      else
                          cout << "s1 and s2 are not equal." << endl;
                      break;
            case '+': s3 = s1 + s2;
                      cout << "s3 has been set to s1 + s2" << endl;
                      break;
            case '*': cout << "Please type a value for x: ";
                      cin >> x;
                      s3 = x * s1;
                      cout << "s3 has been set to " << x << " * s1" <<
endl;
		      break;
            case 'Q': cout << "Ridicule is the best test of truth." <<
endl;
                      break;
            default: cout << choice << " is invalid. Sorry." << endl;
        }
    }
    while ((choice != 'Q'));

    return EXIT_SUCCESS;

}

void print_menu( )
{
    cout << endl;
    cout << "The following choices are available: " << endl;
    cout << " R  Activate one of the reset( ) functions" << endl;
    cout << " 1  Add a new number to the 1st statistician s1" << endl;
    cout << " 2  Add a new number to the 2nd statistician s2" << endl;
    cout << " 3  Add a new number to the 3rd statistician s3" << endl;
    cout << " T  Print a table of values from the statisticians" <<
endl;
    cout << " E  Test whether s1 == s2" << endl;
    cout << " +  Set the third statistician s3 equal to s1 + s2" <<
endl;
    cout << " *  Set the third statistician s3 equal to x*s1" << endl;
    cout << " Q  Quit this test program" << endl;
}

char get_user_command( )
// Library facilties used: iostream.h
{
    char command;

    cout << "Enter choice: ";
    cin >> command;

    return command;
}

double get_number( )
// Library facilties used: iostream.h
{
    double result;

    cout << "Please enter the next real number for the sequence: ";
    cin  >> result;
    cout << result << " has been read." << endl;
    return result;
}

void print_values(const statistician& s)
// Library facilties used: iostream.h
{
    cout << setw(10) << s.length( );
    cout << setw(10) << s.sum( );
    if (s.length( ) != 0)
    {
        cout << setw(10) << s.minimum( );
        cout << setw(10) << s.mean( );
        cout << setw(10) << s.maximum( );
    }
    else
        cout << "      none      none      none";
    cout << endl;
}


The compilation works but when I run the testfile these errors occur:
Code:
------ Build started: Project: Assignment1, Configuration: Debug Win32
------
Compiling...
stattest.cpp
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
stattest.obj : error LNK2019: unresolved external symbol "public: void
__thiscall main_savitch_2C::statistician::reset(void)"
(?reset@statistician@main_savitch_2C@@QAEXXZ) referenced in function
_main
C:\Dokumente und Einstellungen\matti\Eigene Dateien\Visual Studio
2005\Projects\Assignment1\Debug\Assignment1.exe : fatal error LNK1120:
4 unresolved externals
Build log was saved at "file://c:\Dokumente und
Einstellungen\matti\Eigene Dateien\Visual Studio
2005\Projects\Assignment1\Assignment1\Debug\BuildLog.htm"
Assignment1 - 5 error(s), 0 warning(s)

Does anybody know how i can fix that? :((
 
P

pat270881

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

pat270881

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::eek:perator*(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::eek:perator+(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::eek:perator==(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\BuildLog.htm"
Assignment1 - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top