CyanMagentaYellowBlack - 5 months ago 30

C++ Question

The part that is giving me an error message is in the implementation file when I wrote the definition for the friend function that overloads the operator +. It is saying Statistician does not name a type. But it is a friend function and is written in the implementation file where the header is included so I am not sure why it does not recognize this. Also I realize that I spelled statistician wrong for the file name, but don`t know how to easily rename a file in codeblocks.

`//header file`

#ifndef STATISTICIAN_H

#define STATISTICIAN_H

namespace GREGORY_STOCKER_STATICTICIAN{

class Statistician{

public:

Statistician();

void next_number(double);

void erase_sequence();

int get_length() const {return length_sequence;}

double get_sum() const{return sum;}

double get_mean() const;

double get_largest() const;

double get_smallest() const;

double get_last() const;

friend Statistician operator + (const Statistician &,const Statistician &);

private:

int length_sequence;

double sum;

double smallest;

double largest;

double last;

};

#endif

}

//implementation file

using namespace std;

#include "Statictician.h"

#include <iostream>

#include <cassert>

namespace GREGORY_STOCKER_STATICTICIAN{

Statistician :: Statistician()

{

length_sequence = 0;

sum = 0;

smallest = 0;

largest = 0;

last = 0;

}

void Statistician :: next_number(double num)

{

length_sequence += 1;

sum += num;

if(length_sequence == 1)

{

smallest = num;

largest = num;

}

if (num < smallest)

smallest = num;

if (num > largest)

largest = num;

last = num;

}

void Statistician :: erase_sequence()

{

length_sequence = 0;

sum = 0;

smallest =0;

largest = 0;

last = 0;

}

double Statistician :: get_mean () const

{

assert(length_sequence > 0);

return sum / 2;

}

double Statistician :: get_largest() const

{

assert(length_sequence > 0);

return largest;

}

double Statistician :: get_smallest() const

{

assert(length_sequence > 0);

return smallest;

}

double Statistician :: get_last() const

{

assert(length_sequence > 0);

return last;

}

}

//the part that is tripping the error message

Statistician operator +(const Statistician &s1,const Statistician &s2)

{

Statistician s3;

s3.sum = (s1.sum + s2.sum);

s3.sequence_length = (s1.sequence_length + s2.sequence_length;

if(s1. largest > s2.largest)

s3.largest = s1.largest;

else

s3.smallest = s2.smallest;

if(s1. smallest < s2.smallest)

s3.smallest = s1.smallest;

else

s3.smallest = s2.smallest;

s3.last = s2.last;

return s3;

}

Answer

That's because `Statistician`

is a member of `GREGORY_STOCKER_STATICTICIAN`

namespace.

You either have to write

```
GREGORY_STOCKER_STATICTICIAN::Statistician operator +(const GREGORY_STOCKER_STATICTICIAN::Statistician &s1,const GREGORY_STOCKER_STATICTICIAN::Statistician &s2)
```

or

```
using GREGORY_STOCKER_STATISTICIAN::Statistician;
```

or

```
using namespace GREGORY_STOCKER_STATICTICIAN;
```

(but if you do, why put it in a namespace to start with)