【问题标题】:Implementing objects from one class in another在另一个类中实现对象
【发布时间】:2017-04-10 10:15:27
【问题描述】:

我在尝试在类 Cart_Point 中实现类 Cart_Vector 中的对象时遇到了麻烦。我的编译器列出了以下错误,我似乎无法修复它们:

friend Cart_Point operator+(const Cart_Point&p1,const Cart_Vector&v1); 'Cart_Vector' 没有命名类型

Cart_Point 运算符+(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;
}

【问题讨论】:

  • Cart_Point之前定义Cart_Vector,因为后者使用前者,反之则不然。

标签: c++ class object operator-overloading


【解决方案1】:

将您的代码拆分为不同的文件,但您的操作符

#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 :

【讨论】:

    【解决方案2】:

    帮自己一个忙,将代码拆分到不同的文件中,至少一个 .h 用于 Cart_Vector,一个 .h 用于 Cart_Point,一个 .cpp 用于测试脚本(主)。您可以更正此代码以使其正常工作(只需交换两个类的顺序),如果您也更正其他错误,例如运算符“-”的重载。

    这里的重点是,如果您以这种方式开始编码,当您开始编写复杂的项目时,您会发现自己遇到了很大的困难。为每个文件编写一个公共(非内部)类就像对文件应用单一责任原则一样。我想说,一个文件也应该只有一个改变的理由,这对可读性和你什么时候执行版本控制有很大的好处。

    【讨论】:

      【解决方案3】:

      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 对象的非常量元素,也不能调用非常量函数。如果需要,您可以按值传递这些对象。

      【讨论】:

      • 我猜这里有问题 return Cart_Point (x,y) ;应该是正确的
      • 是的,这个函数也不能返回 Cart_Vector (x, y)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      • 2014-05-26
      • 1970-01-01
      • 2018-12-13
      • 2017-08-16
      • 1970-01-01
      相关资源
      最近更新 更多