从另一个类实现对象
Implementing objects from one class in another
我一直在尝试从类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函数。如果需要,您可以按值传递这些对象。
- 如何实现自定义匹配器以检查 Catch2 中的对象相等性
- 我能否通过将函数实现为类对象方法来避免使用互斥锁
- 为什么地图需要实现'operator<'以及如何比较对象?
- CMake + Qt,moc 编译失败,无法实现 QMetaObject 方法(编译器找不到基本 ui 对象的标头?
- C++接口实现和子类化对象识别
- 对象内部有大量数据容器,实现更高效的对象交换
- 使用初始值设定项列表作为函数参数的类对象实现运算符 [] 的示例
- 可选<double>可以作为 8 字节对象实现吗?
- 我们可以在同一对象中实现多个QtRO接口吗?
- 如何在V8中为C 本机对象实现属性枚举器
- 我真的需要为常量对象实现用户提供的构造函数吗?
- C++对象 [实现] 运算符 == 错误
- 使用指针和使用静态对象实现单例实现之间的区别
- 为具有内部指针的对象实现swap()
- 对象实现后动态绑定大多数不相关的对象
- 此同步对象实现线程是否安全
- 如何在c++中为每个对象实现定时器
- 预序列化消息对象-实现
- 使用ADO命令对象实现批量/批量插入
- 用抽象基类重用对象实现多态