简体   繁体   中英

C++ Xcode No matching constructor for initialisation of type vector2d

I have a vector2d class. I am getting the error "No matching constructor for initialisation type" on the following code :

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

My vectors classes declarations and definitions are :

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

I am not creating the object using default constructor argument,then whats the reason for that error? Note that the code ran on Visual Studio perfectly fine. While on Xcode i get the error.

Your problem will be this constructor:

vector2d(vector2d& v) ;

a standard copy constructor looks like this:

vector2d(const vector2d& v) ;

because in standard c++ you cannot bind a temporary to a mutable lvalue-reference

Unfortunately, Microsoft in their wisdom have unleashed a number of "extensions" (ie evil deviations from the standard) into their compiler and in MSVC only, a temporary will bind to a mutable l-value reference.

In real standard c++, a temporary may bind to:

  1. a copy vector2d(vector2d v) ;
  2. a const lvalue-reference vector2d(const vector2d& v) ;
  3. an rvalue-reference vector2d(vector2d&& v) ;

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM