C 如何在另一类中使用操作员过载
C++ how to use operator overload in another class
class vec3{
float x;
float y;
float z;
const vec3 & operator= (const vec3 &rvec3)
{
x = rvec3.x;
y = rvec3.y;
z = rvec3.z;
return *this;
} };
class vec2{
vec3 vetex;
vec3 normal;
const vec2 & operator = (const vec2 &rvec2)
{
vetex = rvec2.vetex;
normal= rvec2.normal;
return *this;
} };
编译器显示错误" Operator ="是'VEC3'的私人成员。怎么会发生?
更改为
class vec3{
float x;
float y;
float z;
public:
const vec3 & operator= (const vec3 &rvec3)
{
x = rvec3.x;
y = rvec3.y;
z = rvec3.z;
return *this;
} };
操作员方法已公开,从外部代码可以看到。
除了类vec2
中operator=
的定义外,这是不必要的,因为它是此类操作员的默认行为。顺便说一句,vec3
中的operator=
也不需要。
也许您应该从类别更改为结构。
默认情况下,C 中的类成员是私有的。
要允许外部代码查看操作员,您需要将其指定为公共。
class vec3{
float x;
float y;
float z;
public:
const vec3 & operator= (const vec3 &rvec3)
{
x = rvec3.x;
y = rvec3.y;
z = rvec3.z;
return *this;
} };
相关文章:
- 如何创建从同一类继承的不同对象的向量
- <<操作员在下面的行中工作
- 获取从C++中同一类中的构造函数调用的方法返回的值
- C++如何将同一类的实例作为属性
- C++ 与操作员不匹配<<
- 操作员C++的模棱两可的过载
- C++中>>操作员过载时出现问题?
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- NaN 上的宇宙飞船操作员
- 如何使用connect将qml按钮与同一类的cpp函数连接起来
- C++交换来自同一类成员的参数值,处理多个类
- 比根<操作员
- 使用同一类中的方法重新分配字段时出现问题
- SFINAE不能防止模棱两可的操作员过载吗?
- 什么是现实中的"endl"(或任何输出操纵器)?它是如何实现的,它如何与操作员<<一起工
- 类成员函数无法访问同一类的私有结构节点?
- 为什么"delete"操作员给我访问权限冲突
- 无法将指向类的成员函数的函数指针作为参数传递给同一类的另一个成员函数
- C 如何在另一类中使用操作员过载
- OpenCV:文件存储操作员">>"在一个项目中工作,但不在另一个项目中工作(在同一类上)