0

Can I replace the using namespace std; from the implementation file with some other syntax in the header file? what would be the best way to include this code in order for cout to work fine?

Here's my header file:

#ifndef LINE_H
#define LINE_H
#include<iostream>
#include<cmath>


    class Line
    {
        public:
    
            Line();
            Line(double x1, double y1, double x2, double y2);
            void setLine(double a1, double b1, double a2, double b2);
            double getLen();
            void printLine();
            ~Line();
    
        private:
    
            double x1,y1,x2,y2,length;
            double Length(double a1, double b1, double a2,double b2);
    
    };
    
    #endif // LINE_H

And here's the implementation file:

#include "Line.h"
using namespace std;


// **********Constructors*****************
    Line::Line() {}

    Line::Line(double x1, double y1, double x2, double y2){
        this-> x1=x1; this-> y1=y1; this->x2=x2; this->y2=y2;
        length = Length(x1,y1,x2,y2);
    }

// ***********Public Methods***********************
    void Line::setLine(double a1, double b1, double a2, double b2){
        x1=a1; y1=b1; x2=a2; y2=b2;
        length = Length(x1,y1,x2,y2);
    }

    double Line::getLen(){
        return length;
    }

    void Line::printLine(){
        cout<<"("<<x1<<","<<y1<<") --> ("<<x2<<","<<y2<<")"<<endl;
    }

// ***********Private Methods***************
    double Length(double a1, double b1, double a2,double b2){       //Private method which calculates the distance between 2 points.
        return sqrt(pow(b2-b1,2)+pow(a2-a1,2));
    }

//  ***********Destructor******************
    Line::~Line(){}
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
  • `double Length(` needs to be `double Line::Length(`. – Daniel Langr Dec 13 '21 at 11:34
  • @DanielLangr Thanks. About the other question - what is the acceptable way of including the "using namespace" ? in the header file? in the implementation file? – DirichletIsaPartyPooper Dec 13 '21 at 11:36
  • 1
    Never ever use the `using` directive in a header file at a file / public namespace scope. For details, see, e.g., https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice, https://stackoverflow.com/questions/5849457/using-namespace-in-c-headers, etc. Generally, `using namespace ...` is usually bad practice per se. – Daniel Langr Dec 13 '21 at 11:39
  • There is no acceptable way of including (or otherwise using) `using namespace`. It is an antipattern; just avoid it. Use `using` declarations without `namespace` inside the innermost (ideally anonymous) `namespace` in source / definition files and, if absolutely necessary, use `private:` member types introduced as shortcuts with `using` in header files. Never ever use an entire namespace; it defeats the purpose of namespaces. – Andrej Podzimek Dec 13 '21 at 12:17
  • The best way is to spell out the fully qualified name for `std::cout` each time you refer to it. – Michaël Roy Dec 13 '21 at 14:20
  • You can import just that one name with `using std::cout;`. But don't do it in the header file. Do it in the implementation file. Also, please don't morph questions. The title of this question no longer matches the body, which will confuse others looking for solutions to "undefined reference" problems. – Raymond Chen Dec 13 '21 at 14:35

1 Answers1

0

As Stated in several comments It is not a good idea to declare using namespace $ in a global scope of a header file.
this is because it forces anyone who include your Header to also be subject to your using...

It is also a good practice to place everything inside a namespace so it wont conflict with other declarations.

combine the two conventions, and Horray you can declare your Line class inside your own namespace (say MyShapes), and within that scope be using namespace std

 #ifndef LINE_H
 #define LINE_H
 #include<iostream>
 #include<cmath>

  using namespace MyShapes
  {
    using namespace std; // this is ok, as it affects only the MyShapes scope
    class Line
    {
        public:
    
            Line();
            Line(double x1, double y1, double x2, double y2);
            void setLine(double a1, double b1, double a2, double b2);
            double getLen();
            void printLine();
            ~Line();
    
        private:
    
            double x1,y1,x2,y2,length;
            double Length(double a1, double b1, double a2,double b2);
    
    };
  }
 #endif // LINE_H

Cheers.

Tomer W
  • 3,395
  • 2
  • 29
  • 44