简体   繁体   中英

Undefined reference problem, c++ class definition

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(){}

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.

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