[英]C++ Xcode No matching constructor for initialisation of type vector2d
我有一個vector2d類。 我在以下代碼中收到錯誤“初始化類型沒有匹配的構造函數”:
vector2d vector2d::operator+(const vector2d& vector)
{
return vector2d((this->x + vector.x), (this->y + vector.y));
}
vector2d vector2d::operator-(const vector2d& vector)
{
return vector2d(this->x - vector.x, this->y - vector.y);
}
我的向量類聲明和定義是:
#ifndef __VECTOR2D_H__
#define __VECTOR2D_H__
class vector2d
{
public:
float x, y , w;
vector2d(const float x, const float y) ;
vector2d(vector2d& v) ;
vector2d operator+(const vector2d& rhs);
vector2d operator-(const vector2d& rhs);
vector2d& operator+=(const vector2d& rhs);
vector2d& operator-=(const vector2d& rhs);
float operator*(const vector2d& rhs);
float crossProduct(const vector2d& vec);
vector2d normalize();
float magnitude();
};
#endif
vector2d.cpp:
#include "vector2d.h"
#include <cmath>
vector2d::vector2d(const float x,const float y) :x(x),y(y),w(1)
{
}
vector2d::vector2d(vector2d& vector) : x(vector.x), y(vector.y), w(1)
{
}
vector2d vector2d::operator+(const vector2d& vector)
{
return vector2d((this->x + vector.x), (this->y + vector.y));
}
vector2d vector2d::operator-(const vector2d& vector)
{
return vector2d(this->x - vector.x, this->y - vector.y);
}
vector2d& vector2d::operator+=(const vector2d& vector)
{
this->x += vector.x;
this->y += vector.y;
return *this;
}
vector2d& vector2d::operator-=(const vector2d& vector)
{
this->x -= vector.x;
this->y -= vector.y;
return *this;
}
float vector2d::magnitude()
{
return sqrt(this->x * this->x + this->y * this->y);
}
//Make Unit Vector
vector2d vector2d::normalize()
{
float magnitude = this->magnitude();
float nx = 0.0f;
float ny = 0.0f;
nx = this->x / magnitude;
ny = this->y / magnitude;
return vector2d(nx,ny);
}
float vector2d::operator*(const vector2d& rhs)
{
return ( (this->x * rhs.x) + (this->y * rhs.y) );
}
float vector2d::crossProduct(const vector2d& vec)
{
return (x * vec.y - y * vec.x);
}
我沒有使用默認構造函數參數創建對象,那么該錯誤的原因是什么? 請注意,代碼在Visual Studio上運行得很好。 在Xcode上時出現錯誤。
您的問題將是此構造函數:
vector2d(vector2d& v) ;
標准副本構造函數如下所示:
vector2d(const vector2d& v) ;
因為在標准c ++中,您不能將臨時綁定到可變的左值引用
不幸的是,微軟以他們的智慧在他們的編譯器中釋放了許多“擴展”(即,與標准的有害偏差),並且僅在MSVC中,臨時變量會綁定到可變的l值引用。
在
實際的
標准c ++中,臨時項可能綁定到:
vector2d(vector2d v) ;
vector2d(const vector2d& v) ;
vector2d(vector2d&& v) ;
`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.