2016-04-22 11 views
0

私はvector2dクラスを持っています。私は、次のコードにエラー "初期化タイプに該当するコンストラクタ" を取得しています:C++ Xcode 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) ; 

標準のコピーコンストラクタは次のようになります:

答えて

1

あなたの問題は、このコンストラクタになりますが、変更可能な左辺値参照に一時的にバインドすることはできません++

vector2d(const vector2d& v) ; 

ための標準的なC言語で

残念ながら、マイクロソフトの知恵のもとでは、多くの「拡張機能」(標準からの逸脱)がコンパイラとMSVCにのみ公開されていますが、一時的に変更可能なl値参照にバインドされます。

本当 標準C++では、一時的に結合することができるまで:

  1. コピーvector2d(vector2d v) ;
  2. のconst左辺値参照vector2d(const vector2d& v) ;
  3. 右辺値参照vector2d(vector2d&& v) ;

`

関連する問題