在另一个类中实现对象

时间:2016-11-26 03:43:43

标签: c++ class object operator-overloading

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

  

朋友Cart_Point操作员+(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;
}

3 个答案:

答案 0 :(得分:1)

将您的代码拆分为不同的文件但您的运算符&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 :

答案 1 :(得分:0)

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元素,也无法调用非const函数。如果需要,可以按值传递这些对象。

答案 2 :(得分:0)

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

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