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


the part giving me error message in implementation file when wrote definition friend function overloads operator +. saying statistician not name type. friend function , written in implementation file header included not sure why not recognize this. realize spelled statistician wrong file name, don`t know how rename 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 tripping 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; } 

a friend declaration in class, declares function in smallest enclosing namespace. friend declaration declares , friends gregory_stocker_statictician::operator +. not declare nor friend ::operator +.

but code tries implement ::operator + outside namespace. makes entirely different function: not found code tries add 2 statisticians together, because code finds namespaced version. further, doesn't compile (as posted error message under arnav borborah's answer): since ::operator + not friend, cannot access private members.

so simplest solution in fact put operator+ definition inside namespace, matches declaration:

namespace gregory_stocker_statictician {     statistician operator +(const statistician &s1,const statistician &s2)     {         // ...     } } 

now don't need qualify statistician either.


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -