2016-04-22 13 views
0

Ich habe eine vector2d-Klasse. Ich erhalte die Fehlermeldung "Kein passender Konstruktor zur Initialisierung Typ" auf den folgenden Code:C++ Xcode Kein passender Konstruktor zur Initialisierung des Typs 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); 
} 

Meine Vektoren Klassen Erklärungen und Definitionen sind:

#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); 
} 

Ich erstelle das Objekt nicht mit dem Standardkonstruktorargument, was ist dann der Grund für diesen Fehler? Beachten Sie, dass der Code in Visual Studio einwandfrei ausgeführt wurde. Während auf Xcode bekomme ich den Fehler.

Antwort

1

Ihr Problem wird dieser Konstruktor sein:

vector2d(vector2d& v) ; 

ein Standard Copykonstruktor wie folgt aussieht:

vector2d(const vector2d& v) ; 

weil in Standard-C++ Sie keine temporäre auf einen veränderbaren lvalue Verweis binden können

Leider haben Microsoft in ihrer Weisheit eine Reihe von "Erweiterungen" (dh böse Abweichungen vom Standard) in ihren Compiler und nur in MSVC entfesselt, Ein temporäres Objekt wird an eine veränderbare l-Wert-Referenz gebunden.

In realen Standard-C++, eine temporäre binden kann:

  1. eine Kopie vector2d(vector2d v) ;
  2. a const lvalue Verweis vector2d(const vector2d& v) ;
  3. rvalue Verweises vector2d(vector2d&& v) ;

`

Verwandte Themen