从另一个类实现对象

Implementing objects from one class in another

本文关键字:对象 实现 另一个      更新时间:2023-10-16

我一直在尝试从类Cart_Point中的Cart_Vector实现对象很难。我的编译器一直在列出以下错误,我似乎无法修复它们:

朋友cart_point Operator (const cart_point& p1,const cart_vector& v1); " cart_vector"不命名类型

cart_point Operator (const cart_point& p1,const cart_vector& v1) " cart_vector"没有类型

x = p1.x v1.x;在" V1"中要求成员'x' 键入'const int'

y = p1.y v1.y;在" v1"中要求成员'y'的请求 键入'const int'

返回cart_vector(x,y);在此范围中未声明" cart_vector"

#include <iostream>
#include <math.h>

using namespace std;
class Cart_Point
{
public:
        double x;
        double y;
        friend class Cart_Vector;
    Cart_Point (double inputx, double inputy);
    friend Cart_Point operator<<(const Cart_Point&p1, const Cart_Point&p2);
    friend Cart_Point operator+(const Cart_Point&p1,const Cart_Vector&v1);
    friend Cart_Point operator-(const Cart_Point&p1,const Cart_Point&p2);
    double Cart_distance(Cart_Point, Cart_Point);

};
Cart_Point::Cart_Point(double inputx, double inputy)
{
   x = inputx;
   y = inputy;
}
double Cart_Point::Cart_distance(Cart_Point p1, Cart_Point p2)
{
    double distance = (sqrt( pow(p1.x - p2.x,2) + pow(p1.y - p2.y,2) ));
    return distance;
//returns distance between p1 (point 1) and p2 (point 2)
}
Cart_Point operator<<(const Cart_Point&p1, const Cart_Point&p2)
{
    cout << "p1:(" << p1.x << ", " << p1.y << ")" << endl;
    cout << "p2:(" << p2.x << ", " << p2.y << ")" << endl;
    return p1,p2;
//this function should just print each point
}
Cart_Point operator+(const Cart_Point&p1, const Cart_Vector&v1)
{
    double x,y;
    x = p1.x + v1.x;
    y = p1.y + v1.y;
    return Cart_Point(x,y);
//this function should make a new Cart_Point 
}
Cart_Point operator-(const Cart_Point&p1, const Cart_Point&p2)
{
    double x,y;
    x = p1.x- p2.x;
    y = p1.y - p2.y;
    return Cart_Vector(x,y);
//this function should make a new Cart_Vector
}
    class Cart_Vector
{
public:
    double x; //x displacement of vector
    double y; //y displacement of vector
    Cart_Vector(double inputx, double inputy);
    friend Cart_Vector operator*(const Cart_Vector&v1, double d);
    friend Cart_Vector operator/(const Cart_Vector&v1, double d);
    Cart_Vector operator<<(const Cart_Vector&v1);
    friend class Cart_Point;
};
Cart_Vector::Cart_Vector(double inputx, double inputy)
{
    x = inputx;
    y = inputy;
}
Cart_Vector operator*(const Cart_Vector&v1, double d)
{
    double x,y;
    x = v1.x*d;
    y = v1.y*d;
    return Cart_Vector(x,y);
//this function should make a new Cart_Vector
}
Cart_Vector operator/(const Cart_Vector&v1, double d)
{
  double x,y;
  if (d == 0)
  {
      x = v1.x;
      y = v1.y;
  }
  x = v1.x/d;
  y = v1.y/d;
  return Cart_Vector(x,y);
//this function should make a new Cart_Vector and dividing by zero creates v1
}
Cart_Vector Cart_Vector::operator<<(const Cart_Vector&v1)
{
    cout <<"v1: <" << v1.x << ", " << ">" << endl;
    return v1;
//this function should just print v1
}

//TestCheckpoint1.cpp file below
int main()
{
//I haven't finished the main function to test all the functions yet
    return 0;
}

在不同的文件中拆分代码,但是您实现了操作员&lt;&lt;是错误的,请考虑遵循代码,这只是帮助

的提示。
#include <iostream>
#include <math.h>

using namespace std;

class Cart_Vector
{
public:
double x; //x displacement of vector
double y; //y displacement of vector
Cart_Vector(double inputx, double inputy);
friend Cart_Vector operator*(const Cart_Vector&v1, double d);
friend Cart_Vector operator/(const Cart_Vector&v1, double d);
friend std::ostream& operator<<( std::ostream& out,const Cart_Vector&v1);
friend class Cart_Point;
};

Cart_Vector::Cart_Vector(double inputx, double inputy)
{
    x = inputx;
    y = inputy;
}
Cart_Vector operator*(const Cart_Vector&v1, double d)
{
    double x,y;
    x = v1.x*d;
    y = v1.y*d;
    return Cart_Vector(x,y);
//this function should make a new Cart_Vector
}
Cart_Vector operator/(const Cart_Vector&v1, double d)
{
  double x,y;
  if (d == 0)
  {
      x = v1.x;
      y = v1.y;
  }
  x = v1.x/d;
  y = v1.y/d;
  return Cart_Vector(x,y);
//this function should make a new Cart_Vector and dividing by zero creates v1
}
 std::ostream& operator<<(std::ostream &out, const Cart_Vector&v1)
{
   out <<"v1: <" << v1.x << ", " << ">" << endl;
    return out;
//this function should just print v1
}

class Cart_Point
{
public:
        double x;
        double y;
        friend class Cart_Vector;
    Cart_Point (double inputx, double inputy);
    friend std::ostream& operator<<(std::ostream& out , const Cart_Point&p2);
    friend Cart_Point operator+(const Cart_Point&p1,const Cart_Vector&v1);
    friend Cart_Point operator-(const Cart_Point&p1,const Cart_Point&p2);
    double Cart_distance(Cart_Point, Cart_Point);

};
Cart_Point::Cart_Point(double inputx, double inputy)
{
   x = inputx;
   y = inputy;
}
double Cart_Point::Cart_distance(Cart_Point p1, Cart_Point p2)
{
    double distance = (sqrt( pow(p1.x - p2.x,2) + pow(p1.y - p2.y,2) ));
    return distance;
//returns distance between p1 (point 1) and p2 (point 2)
}


std::ostream&  operator<<(std::ostream &out, const Cart_Point&p1)
{
    // Since operator<< is a friend of the Cart_Point class, we can access Point's members directly.
    out << "p:(" << p1.x << ", " << p1.y << ")" << endl;
    return out;
//this function should just print each point
}
Cart_Point operator+(const Cart_Point&p1, const Cart_Vector&v1)
{
    double x,y;
    x = p1.x + v1.x;
    y = p1.y + v1.y;
    return Cart_Point(x,y);
//this function should make a new Cart_Point
}
Cart_Point operator-(const Cart_Point&p1, const Cart_Point&p2)
{
    double x,y;
    x = p1.x- p2.x;
    y = p1.y - p2.y;
    return Cart_Point(x,y);
//this function should make a new Cart_Vector
}

//TestCheckpoint1.cpp file below
int main()
{
Cart_Point point1(2.0, 3.0);
    std::cout << point1;

//I haven't finished the main function to test all the functions yet
    return 0;
}

wandbox:

帮个忙,将代码在不同的文件中拆分,至少一个。您可以纠正此代码以使其正常工作(只需交换两个类的顺序),如果您也更正其他错误,例如操作员的重载中的" - "。

这里的重点是,如果您开始以这种方式进行编码,当您开始编程复杂项目时,您会发现自己很困难。编码一个公共(非inner)class-plum文件也就像将单个责任原则应用于文件一样。我要说的是,一个文件也只有一个理由更改,这在 READABIDABY 中都具有很大的好处,并且您何时将执行版本控制

Div>

cart_vector和cart_point需要彼此。因此,您需要在单独的标题文件中实现这些类。不要忘记包括它们。也在这里;

Cart_Point operator-(const Cart_Point&p1, const Cart_Point&p2)
{
    double x,y;
    x = p1.x- p2.x;
    y = p1.y - p2.y;
    //return Cart_Vector(x,y);
    return Cart_Pointer(x,y);
//this function should make a new Cart_Vector
}

您无法访问const对象的非符合元素,也无法调用非const函数。如果需要,您可以按值传递这些对象。