hello,
i should implement this class: -
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 5 3501
pat270881 wrote:
i should implement this class: -
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
pat270881 wrote:
hello,
i should implement this class: -
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-lit....html#faq-13.9
Cheers! --M
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 -
#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: -
#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:
The compilation works but when I run the testfile these errors occur: -
------ 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? :((
the namespace of the testfile is also test_1 and not main_savitch...,
that was a mistake in the posting...
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
========== This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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.
|
by: Ike Naar |
last post by:
Given the following C++ snippet:
template < typename T > class A { // 1
public : // 2
class AA { } ; // 3
} ; ...
|
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...
|
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:
|
by: Mark P |
last post by:
Is this legal and sensible?
class Outer
{
friend struct Inner
{
...
};
...
|
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"; }
};
|
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!
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
| |