Search code examples
c++namespacesoperator-overloadingfriend

Getting error message saying my class does not name a type when trying to overload the "+" operator


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;
}

Solution

  • A friend declaration in a class, declares the function in the smallest enclosing namespace. So your friend declaration actually declares and friends GREGORY_STOCKER_STATICTICIAN::operator +. It does not declare nor friend ::operator +.

    But your code tries to implement ::operator + outside the namespace. This makes for an entirely different function: it will not be found by any code which tries to add two Statisticians together, because that code only finds the namespaced version. Further, it doesn't even compile (as you posted the error message under Arnav Borborah's answer): since ::operator + is not a friend, it cannot access the private members.

    So the simplest solution is in fact to put the operator+ definition inside the namespace, so that it matches the declaration:

    namespace GREGORY_STOCKER_STATICTICIAN
    {
        Statistician operator +(const Statistician &s1,const Statistician &s2)
        {
            // ...
        }
    }
    

    Now you don't need to qualify Statistician either.