c++ - 在命名空间 C++ 中创建类

标签 c++

我正在尝试在命名空间内创建一个类,因为它是分配的参数。但是,我在命名空间中调用函数时遇到了一些问题,更具体地说是运算符函数。我附上了下面的代码。

点.h

#ifndef HW_2_6_POINT_H
#define HW_2_6_POINT_H
#include <iostream>
using namespace std;


namespace udbhavAg
{
           class Point
                   {

           private:
               double m_x; //private variables x & y
               double m_y;

           public:
               Point(); //default constructor
               Point(double inputX, double inputY); //Overloaded contructor.
               Point(const Point &obj); //copy constructor
               explicit Point(double value);
               ~Point(); //destructor
               double X() const; //getter fucntions
               double Y() const;
               void X(const double& input); //setter functions
               void Y(const double& input);
               string toString() const; //to string fuucntion
               double Distance() const; //distance to the origin
               double Distance(Point p) const; //distance between two points
               Point operator - () const; // Negate the coordinates.
               Point operator * (double factor) const; // Scale the coordinates.
               Point operator + (const Point& p) const; // Add coordinates.
               bool operator == (const Point& p) const; // Equally compare operator.
               Point& operator = (const Point& source); // Assignment operator.
               Point& operator *= (double factor); // Scale the coordinates & assign.

                   };

}



#endif //HW_2_6_POINT_H


点数.cpp
//
// Created by Udbhav Agarwal on 18/05/20.
//

#include "Point.h"
#include <iostream>
#include <math.h>
using namespace std;

namespace udbhavAg
{

        Point::Point() {} //defualt contructor

        Point::Point(double inputX, double inputY) //paramter construcotr, overider
        {
            m_x = inputX;
            m_y = inputY;
        }

        Point::Point(const Point &obj)//copy constructor
        {
            m_x = obj.m_x;
            m_y = obj.m_y;
        }

        Point::~Point()
        {
        } // destructor

        void Point::X(const double& input)  // setting x
        {
            m_x = input;
        }
        void Point::Y(const double& input)  // setting y
        {
            m_y=input;
        }

        double Point::X() const  //returning x
        {
            return m_x;
        }

        double Point::Y() const //returing y
        {
            return m_y;
        }

        string Point::toString() const
        {
            //building the string for output.
            string output = "Point(" + to_string(m_x) + "," + to_string(m_y) + ")";
            return output ;
        }

        double Point::Distance(Point p) const //calculating the distance between 2 points
        {
            double distance = sqrt((pow(p.Y()-m_y,2))+(pow(p.X()-m_x,2)));
            return distance;

        }

        double Point::Distance() const //calculating the distance from origin
        {
            double distance = sqrt((pow(m_x,2))+(pow(m_y,2)));
            return distance;
        }

        Point Point::operator*(double factor) const
        {
            return Point(m_x*factor,m_y*factor);
        }

        Point & Point::operator*=(double factor)
        {
            m_x*=factor;
            m_y*=factor;

            return *this;
        }

        Point Point::operator+(const Point &p) const
        {

            return Point(m_x + p.m_x,m_y + p.m_y);
        }

        Point Point::operator-() const
        {
            return Point(-m_x,-m_y);
        }

        Point & Point::operator=(const Point &source)
        {
            if(this == &source)
            {
                return *this;
            }
            m_x = source.m_x;
            m_y = source.m_y;

            return *this;

        }

        bool Point::operator==(const Point &p) const
        {
            if(this ==&p)
            {
                return true;
            }
            if(this->m_y == p.m_y && this->m_x == p.m_x)
            {
                return true;
            }

            else
            {
                return false;
            }
        }

        ostream &operator<<(ostream &os, const Point &p)
        {
            os <<"Point(" << p.X() <<"," << p.Y() << ")\n";
            return os;
        }

        Point::Point(double value)
        {
            m_x = value;
            m_y = value;
        }



}

主要的()
#include <iostream>
#include "Point.h"
using namespace std;
using namespace udbhavAg;
int main()
{
    udbhavAg::Point p = udbhavAg::Point(1);
    cout << p;
}

编译器抛出错误:错误:二进制表达式的操作数无效('std::__1::ostream'(又名'basic_ostream')和'udbhavAg::Point')
cout << p;


请帮忙。我无法理解为什么我无法在此处访问 << 运算符。当我不将 Point 类放在命名空间中时,它可以正常工作。

最佳答案

在命名空间中创建类没有任何问题。

这不是暴露的错误的含义:

error: invalid operands to binary expression ('std::__1::ostream' (aka 'basic_ostream') and 'udbhavAg::Point') cout << p;



该错误表明编译器难以解析流运算符
    udbhavAg::Point p = udbhavAg::Point(1);
    cout << p;

乍一看,这可能令人惊讶,因为它是在 Points.cpp 中定义的。 :
space udbhavAg
{


        ostream &operator<<(ostream &os, const Point &p)
        {
            os <<"Point(" << p.X() <<"," << p.Y() << ")\n";
            return os;
        }


}

然而,再看一眼,我意识到 Point.h 中没有原型(prototype)。 (或 #include 中的任何其他文件 Main.cpp d)。

因此,流运算符可用于 class Point 的唯一文件(又名翻译单元)是文件Points.cpp .

为了解决这个问题,我会将原型(prototype)添加到 Point.h :
       std::ostream &operator<<(std::ostream &os, const Point &p);

关于c++ - 在命名空间 C++ 中创建类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61862965/

相关文章:

c++ - 我如何(干净利落地!)将 std::stringstream 子类化以进行自定义?

c++ - 如何从复杂算法中提取事件代码路径

c++ - 用于 C++ 的 Eclipse 自动完成迭代器

c++ - 尝试不阻塞地读取键盘输入(Windows、C++)

c++ - 在 C++ winsock 中处理多个客户端的方法

c++ - 为什么 operator = 返回 *this?

c++ - 如何将着色器嵌入到 UWP-DLL 库中?

c++ - OMP : How to find the right size of cache at runtime

c++ - SFML 粒子系统架构

c# - Visual Studio 2012 宏使用 VS2010 值