运算符的两个不同结果 - 在 c++11 中
Two different results with operator - in c++11
我对结果有问题。事实上,我创建了一个类 Vector,然后重新定义了运算符。但是,对于同一操作,运算符"-"给了我两个不同的结果。当我打印操作时,它给了我很好的答案,但是当我想将结果存储在变量中时,结果是不同的。
我错过了班上的操作员吗?
有我的主要:
#include "../include/Vecteur.hpp"
int main(){
vec3f v0(1.0f, 1.0f, 1.0f);
vec3f v1(1.0f, 0.0f, 0.0f);
v1.x() += 3.0f;
v0[1] -= 1.0f;
cout << "v0 = " << v0 << endl;
cout << "v1 = " << v1 << endl;
float l = v0.length();
v1 *= l;
cout << "l = " << l << endl;
cout << "v0 = " << v0 << endl;
cout << "v1 = " << v1 << endl;
auto v2 = v0 + v1;
cout << "v2 = " << v2 << endl;
auto v3 = v0 - v1; // The problem is there.
cout << "v0 = " << v0 << endl;
cout << "v1 = " << v1 << endl;
cout << "v3 = " << v3 << endl; // Prints [1,0,1] instead of [-4.65685, 0, 1]
cout << v0-v1 << endl; // Prints [-4.65685, 0, 1]
if (v2 < v3){
v2 += v3;
}
cout << "v2 = " << v2 << endl;
return 0;
}
有我的课:
#include <iostream>
#include <math.h>
using namespace std;
class vec3f{
private:
float tableau[3];
public:
vec3f(){}
vec3f(float f1, float f2, float f3){
this->tableau[0] = f1;
this->tableau[1] = f2;
this->tableau[2] = f3;
}
float x() const{
return this->tableau[0];
}
float y() const{
return this->tableau[1];
}
float z() const{
return this->tableau[2];
}
float & x(){
return this->tableau[0];
}
float & y(){
return this->tableau[1];
}
float & z(){
return this->tableau[2];
}
float length() const{
return sqrt(( pow(this->tableau[0],2) + pow(this->tableau[1],2) + pow(this->tableau[2],2) ));
}
/* Redifinition of operators */
vec3f operator+(vec3f const & v){
this->tableau[0] += v.x();
this->tableau[1] += v.y();
this->tableau[2] += v.z();
return *this;
}
vec3f operator-(vec3f const & v){
this->tableau[0] -= v.x();
this->tableau[1] -= v.y();
this->tableau[2] -= v.z();
return *this;
}
vec3f operator*=(float const & f){
this->tableau[0] *= f;
this->tableau[1] *= f;
this->tableau[2] *= f;
return *this;
}
bool operator<(vec3f const & v){
return (this->tableau[0]<v.x() && this->tableau[1]<v.y() && this->tableau[2]<v.z());
}
vec3f & operator=(vec3f const & v){
return *this;
}
vec3f operator=(vec3f const & v) const{
return *this;
}
float operator[](size_t i) const{
return tableau[i];
}
float & operator[](size_t i){
return tableau[i];
}
};
ostream & operator<<(ostream & out, vec3f const & v){
out << "[" << v.x() << ", " << v.y() << ", " << v.z() << "]";
return out;
}
vec3f operator+(vec3f const & va, vec3f const & vb){
return vec3f(va.x()+vb.x(),va.y()+vb.y(),va.z()+vb.z());
}
vec3f operator-(vec3f const & va, vec3f const & vb){
return vec3f(va.x()-vb.x(),va.y()-vb.y(),va.z()-vb.z());
}
vec3f operator+=(vec3f const & va, vec3f const & vb){
return vec3f(va.x()+vb.x(),va.y()+vb.y(),va.z()+vb.z());
}
谢谢:)
在类中,您可能希望定义运算符+=
和-=
(就像您为 *=
所做的那样(。但是你忘记了=
- 并定义了+
,-
作为修改操作数的成员函数。
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- valgrind-hellgrind与泄漏检查的结果不同
- 用C++20 fmt限制结果的总大小
- 使用std::tie进行类似golang的错误处理,同时返回结果,是否有缺点?(C++11)
- 运算符的两个不同结果 - 在 c++11 中
- 不等式比较结果 移至 C++11 时未使用的警告
- 代码块 10.05 和 12.11、13.12 使用相同的代码返回不同的结果
- 关于C++11内存模型的奇怪结果(放松排序)
- 编译一个相当简单的c++11程序时,gcc和clang之间的结果不同
- 将 C++11 正则表达式与 gcc 4.8.2 一起使用时会出现奇怪的结果(但适用于 Boost 正则表达式)
- 欧拉#11项目每次运行时都会产生不同的结果
- 使用 c++11 线程返回结果的正确方法是什么
- 当试图为项目euler 11找到数组中最大的产品时,得到了尴尬的结果
- 在C++11中,未排序的修改警告变为结果未使用的警告
- 运算符=(std::p romise&&) 在 C++11 中的结果是什么?
- 复制c++ 11随机生成器的相同结果
- 递增后的结果应该是11,是12,为什么?
- 如何使用c++ 11初始化函数结果的const向量?
- C++11 MinGW 4.9.1 shared_ptr和常量静态类字段结果"Undefined reference"
- 获取c++11中函数的结果类型