简体   繁体   中英

C++ ostream overload on separated cpp

I am new to C++ and I am reading Stroustrup's book but I have a problem with the overload of an operator for a class. I have these 3 files:

在此处输入图片说明

I have two files as usual and in the header I have the 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 with the error
   class EquationError {

    private:
     char* msg;
     int error_number;

    public:
     char* getMsg() { return msg; }
     EquationError(char* a = "Error.", int err = 0) {
      msg = a;
      error_number = err;
     };

   };

   //std::ostream& operator << (const std::ostream& x, eqSecDeg& A);
   // ^ is commented but it gives the error that it should be declared with only 1 parameter (?)    

 };

}

#endif

Here you can see the implementation of the 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 be zero.", 1);
  }

  a = valA;
  b = valB;
  c = valC;

 }

 std::vector<double> eqSecDeg::getSolutions() {

  double delta = getDelta(a,b,c);

  if (delta >= 0) {

   //x1 real and complex
   solArray.push_back( (-b+sqrt(delta))/(2*a) );
   solArray.push_back(0);
   //x2 real and complex
   solArray.push_back( (-b-sqrt(delta))/(2*a) );
   solArray.push_back(0);

  } else {

   delta *= -1;

   //x1 real and complex
   solArray.push_back( -b/(2*a) );
   solArray.push_back( (sqrt(delta)/(2*a)) );
   //x2 real and 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 are a lot of answers on SO and google but I wasn't able to find the correct one. I am trying to understand how I have to use the operator overload. I guess that declaring the operator overload in the header file and its implementation on the cpp correct.

  • Why is the compiler telling me that there are too many parameters? I am sure (?) that I need 2 of them (the ostream and the class)
  • I know that the friend keyword only gives the access to all members without making the method be a part of the class. When I use that keyword the compiler tells me that there isn't a valid definition of ostream output for int/double/char/whatever. Where should I place the operator overload definition?

I can't understant where (and how) I have to implement it. Any suggestion?


Not very relevant but the main does something like this:

//a, b, c taken from the cin istream
equations::eqSecDeg solver(a,b,c);
std::vector<double> soluzioni = solver.getSolutions();

//here I'd like to call this
std::cout << solver;
  1. Declare the function as a non-member function in the .h file.

     std::ostream& eqSecDeg::operator<<(std::ostream& x, eqSecDeg const& A); 
  2. Implement the function in the .cpp file, like you have. Update it to use const& instead of non-const & .

  3. Use it anywhere you like.

     eqSecDeg A; ... std::cout << A << std::endl; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM