【发布时间】:2020-05-18 05:28:37
【问题描述】:
我正在尝试在命名空间内创建一个类,因为它是分配的参数。但是,我在命名空间中调用函数时遇到了一些问题,更具体地说是运算符函数。我附上了下面的代码。
点.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
请帮忙。我无法理解为什么我无法在此处访问
【问题讨论】:
-
您没有接受 const int 的 Point 构造函数。您有接受两个双精度数和一个点类型但不接受整数的构造函数,请尝试在 1 之后添加 .0。
-
@SPlatten 暴露的错误与构造函数无关。这是关于流运算符
operator<<()。 -
有一个
std::ostream& operator<<(std::ostream&, const Point&)的定义,但它“隐藏”在Points.cpp中。在Point.h中添加它的声明以解决您的问题。 -
我们经常看到这种情况,新手学习新东西(在这种情况下是命名空间),所以任何问题都会归咎于命名空间。事实上它只是一个缺少的声明,不管有没有命名空间都会导致错误。
-
@Scheff 哦,是的,这完全可以理解,尤其是当您对正在学习的所有新内容感到有点不知所措时。但是作为程序员,你必须学习的一件事就是不要对你的代码做太多假设。
标签: c++