Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ overloading an operator double * Vec
    primarykey
    data
    text
    <p>I'm trying to make a Vec class to represent a set of 3 doubles, x, y, z, respectively. What I would like to do is make it so that I can multiply a scalar times the vector and have it multiply each component. </p> <p>I have been able to get it to work when I mutliply a vector by a scalar, but not when I do the reverse. For example, the following works:</p> <pre><code>Vec a = Vec(1.0, 1.0, 1.0); Vec b = a * 2.0; </code></pre> <p>But, whenever I try to multiply a scalar by a vector, it doesn't work. Ideally the command would look like this:</p> <pre><code>Vec a = Vec(1.0, 1.0, 1.0); Vec b = 2.0 * a; </code></pre> <p>Here is the code I've done so far:</p> <pre><code>#include "Vec.h" #include &lt;limits&gt; #include &lt;cmath&gt; #include "constants.h" #include &lt;iostream&gt; #include &lt;string&gt; double Vec::angle( const Vec &amp; vec) { return acos((this-&gt;dot(vec))/(this-&gt;mag() * mag())); } double Vec::angle_d(const Vec &amp; vec) { return (angle(vec) * _PI / 180.0); } double Vec::angle_r(const Vec &amp; vec) { return this-&gt;angle(vec); } Vec Vec::cross( const Vec&amp; vec) { return Vec( (y * vec.z - z * vec.y), (z * vec.x - x * vec.z), (x * vec.y - y * vec.x)); } double Vec::dot( const Vec &amp; vec) { return (x * vec.x + y * vec.y + z * vec.z); } double Vec::mag() { return std::sqrt(x*x + y*y + z*z); } Vec Vec::operator=(const Vec&amp; rhs) { return Vec(rhs); } Vec Vec::operator*(const Vec&amp; rhs) { return Vec( x * rhs.x, y * rhs.y, z * rhs.z); } Vec Vec::operator*(const double rhs) { return Vec(rhs * x, rhs * y, rhs * z); } Vec::Vec() : x(std::numeric_limits&lt;double&gt;::signaling_NaN()), y(std::numeric_limits&lt;double&gt;::signaling_NaN()), z(std::numeric_limits&lt;double&gt;::signaling_NaN()) { } Vec::Vec( double c) : x(c), y(c), z(c) {} Vec::Vec(const Vec &amp; vec) : x(vec.x), y(vec.y), z(vec.z) { } Vec::Vec(double a, double b, double c) : x(a), y(b), z(c) { } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload