2016-11-26 154 views
0

我一直在尝试实现类Cart_Vector中类Cart_Point中的对象时遇到问题。我的编译器已经上市了以下错误,我似乎无法修复它们:实现另一个类中的对象

朋友Cart_Point运营商+(常量Cart_Point & P1,常量Cart_Vector & V1); 'Cart_Vector' 没有指定类型

Cart_Point操作者+(常量Cart_Point & P1,常量Cart_Vector & V1) 'Cart_Vector' 没有一个类型

X = p1.x + 1.x版;在'v1'中请求成员'x',这是非类 类型'const int'

y = p1.y + v1.y;在'v1'中请求成员'y',这是非类 类型'const int'

return 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; 
} 
+0

之前'Cart_Point'定义'Cart_Vector',因为后者使用前者,但反之亦然。 –

回答

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函数。如果需要,可以通过值传递这些对象。

+0

我想这里有一个问题返回Cart_Point(x,y);是否正确 – Adib

+0

是的,此功能也不能返回Cart_ Vector(x,y)。 – mcemilg

0

帮你一个忙,把你的代码分成不同的文件,至少有一个用于Cart_Vector的.h,一个用于Cart_Point的.h,以及用于测试脚本(main)的一个.cpp。您可以更正此代码以使其工作(只需交换两个类的顺序),如果您更正了其他错误,例如运算符“ - ”的超载。

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

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 :

相关问题