operator overloading - C++ ostream overload on separated cpp -


i new c++ , reading stroustrup's book have problem overload of operator class. have these 3 files:

enter image description here

i have 2 files usual , in header have definitions:

#ifndef equations_h #define equations_h  #include<vector>  namespace equations {   //solve second degree equation  class eqsecdeg {    private:    double a;    double b;    double c;    std::vector<double> solarray;    double getdelta(double a, double b, double c);    public:    eqsecdeg(const double vala, const double valb, const double valc);    double getdelta();    std::vector<double> getsolutions();     //thrown error    class equationerror {      private:      char* msg;      int error_number;      public:      char* getmsg() { return msg; }      equationerror(char* = "error.", int err = 0) {       msg = a;       error_number = err;      };     };     //std::ostream& operator << (const std::ostream& x, eqsecdeg& a);    // ^ commented gives error should declared 1 parameter (?)       };  }  #endif 

here can see implementation of dqsecdegree.cpp:

#include<math.h> #include "dqsecdegree.h"  namespace equations {   double eqsecdeg::getdelta(double a, double b, double c) {   return (b*b)-(4*a*c);  }   double eqsecdeg::getdelta() {   return (b*b)-(4*a*c);  }   //constructor  eqsecdeg::eqsecdeg(const double vala, const double valb, const double valc) {    if (vala == 0) {    throw equationerror("parameter 'a' cannot zero.", 1);   }    = vala;   b = valb;   c = valc;   }   std::vector<double> eqsecdeg::getsolutions() {    double delta = getdelta(a,b,c);    if (delta >= 0) {     //x1 real , complex    solarray.push_back( (-b+sqrt(delta))/(2*a) );    solarray.push_back(0);    //x2 real , complex    solarray.push_back( (-b-sqrt(delta))/(2*a) );    solarray.push_back(0);    } else {     delta *= -1;     //x1 real , complex    solarray.push_back( -b/(2*a) );    solarray.push_back( (sqrt(delta)/(2*a)) );    //x2 real , complex    solarray.push_back( -b/(2*a) );    solarray.push_back( -(sqrt(delta)/(2*a)) );    }    return solarray;   }   std::ostream& eqsecdeg::operator << (std::ostream& x, eqsecdeg& a) {   return x << "something here";  }  } 

there lot of answers on , google wasn't able find correct one. trying understand how have use operator overload. guess declaring operator overload in header file , implementation on cpp correct.

  • why compiler telling me there many parameters? sure (?) need 2 of them (the ostream , class)
  • i know friend keyword gives access members without making method part of class. when use keyword compiler tells me there isn't valid definition of ostream output int/double/char/whatever. where should place operator overload definition?

i can't understant (and how) have implement it. suggestion?


not relevant main this:

//a, b, c taken cin istream equations::eqsecdeg solver(a,b,c); std::vector<double> soluzioni = solver.getsolutions();  //here i'd call std::cout << solver; 

  1. declare function non-member function in .h file.

    std::ostream& eqsecdeg::operator<<(std::ostream& x, eqsecdeg const& a); 
  2. implement function in .cpp file, have. update use const& instead of non-const &.

  3. use anywhere like.

    eqsecdeg a;  ...  std::cout << << std::endl; 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -