'C++ unknown error in vector calculation function
I am quite new to programming, I started with c# and now I want to learn c++. I started my first project with some mathematical functions. I wanted to be able to add subtract etc. vectors. I did the vec2 thing and everything works fine. Then I just copied the files and updated them to vec3 and vec4. I just added a third and fourth axis (z, w). I am now getting a bunch of errors and I literally have no idea what they mean xD I hope someone can help me This is my code for the vec3's (the vec2 and vec4 is basically the same, just one more vector more or less):
#include "vec3.h"
#include <iostream>
namespace nexus {
namespace maths {
vec3::vec3() {
x = 0.0f;
y = 0.0f;
z = 0.0f;
}
vec3::vec3(const float& x, const float& y, const float& z) {
this->x = x;
this->y = y;
this->y = z;
}
vec3& vec3::add(const vec3& other) {
x += other.x;
y += other.y;
z += other.z;
return *this;
}
vec3& vec3::subtract(const vec3& other) {
x -= other.x;
y -= other.y;
z -= other.z;
return *this;
}
vec3& vec3::multiply(const vec3& other) {
x *= other.x;
y *= other.y;
z *= other.z;
return *this;
}
vec3& vec3::divide(const vec3& other) {
x /= other.x;
y /= other.y;
z /= other.z;
return *this;
}
vec3 operator+(vec3 left, const vec3& right) {
return left.add(right);
}
vec3 operator-(vec3 left, const vec3& right) {
return left.subtract(right);
}
vec3 operator*(vec3 left, const vec3& right) {
return left.multiply(right);
}
vec3 operator/(vec3 left, const vec3& right) {
return left.divide(right);
}
vec3& vec3::operator+=(const vec3& other) {
*this = *this + other;
return *this;
}
vec3& vec3::operator-=(const vec3& other) {
*this = *this - other;
return *this;
}
vec3& vec3::operator*=(const vec3& other) {
*this = *this * other;
return *this;
}
vec3& vec3::operator/=(const vec3& other) {
*this = *this / other;
return *this;
}
bool vec3::operator==(const vec3& other) {
return x == other.x && y == other.y && z == other.z;
}
bool vec3::operator!=(const vec3& other) {
return !(*this == other);
}
std::ostream& operator<<(std::ostream& stream, const vec3& vector) {
stream << "vec3: (" << vector.x << ", " << vector.y << ", " << vector.z << ")";
return stream;
}
} }
And the header:
#pragma once
#include <iostream>
namespace nexus {
namespace maths {
struct vec3 {
float x, y, z;
vec3();
vec3(const float& x, const float& y, const float& z);
vec3 add(const vec3& other);
vec3 subtract(const vec3& other);
vec3 multiply(const vec3& other);
vec3 divide(const vec3& other);
friend vec3& operator+(vec3 left, const vec3& right);
friend vec3& operator-(vec3 left, const vec3& right);
friend vec3& operator*(vec3 left, const vec3& right);
friend vec3& operator/(vec3 left, const vec3& right);
vec3& operator+=(const vec3& other);
vec3& operator-=(const vec3& other);
vec3& operator*=(const vec3& other);
vec3& operator/=(const vec3& other);
bool operator==(const vec3& other);
bool operator!=(const vec3& other);
friend std::ostream& operator<<(std::ostream& stream, const vec3& vector);
};
} }
Solution 1:[1]
You made some simple typos. That's all.
All propblems are resulting from function signatures / declaration in your struct definition being not the same as in the definition part.
For example:
vec3 add(const vec3& other);
in the declaration and vec3& vec3::add(const vec3& other)
in the definition of the function. The retuen type is different. See the mssing &
. So, one time a reference and the other time a value is used.
This is valid for nearly all functions.
Please check always for the correct and identical function signatures.
There is some other small type in the constructor:
this->y = z;
should be this->z = z;
All these problems will be repotrted by the compiler.
Anyway, just small typos. Please see the corrected code below:
#include <iostream>
struct vec3 {
float x, y, z;
vec3();
vec3(const float& x, const float& y, const float& z);
vec3& add(const vec3& other);
vec3& subtract(const vec3& other);
vec3& multiply(const vec3& other);
vec3& divide(const vec3& other);
friend vec3& operator+(vec3 left, const vec3& right);
friend vec3& operator-(vec3 left, const vec3& right);
friend vec3& operator*(vec3 left, const vec3& right);
friend vec3& operator/(vec3 left, const vec3& right);
vec3& operator+=(const vec3& other);
vec3& operator-=(const vec3& other);
vec3& operator*=(const vec3& other);
vec3& operator/=(const vec3& other);
bool operator==(const vec3& other);
bool operator!=(const vec3& other);
friend std::ostream& operator<<(std::ostream& stream, const vec3& vector);
};
vec3::vec3() {
x = 0.0f;
y = 0.0f;
z = 0.0f;
}
vec3::vec3(const float& x, const float& y, const float& z) {
this->x = x;
this->y = y;
this->z = z;
}
vec3& vec3::add(const vec3& other) {
x += other.x;
y += other.y;
z += other.z;
return *this;
}
vec3& vec3::subtract(const vec3& other) {
x -= other.x;
y -= other.y;
z -= other.z;
return *this;
}
vec3& vec3::multiply(const vec3& other) {
x *= other.x;
y *= other.y;
z *= other.z;
return *this;
}
vec3& vec3::divide(const vec3& other) {
x /= other.x;
y /= other.y;
z /= other.z;
return *this;
}
vec3& operator+(vec3 left, const vec3& right) {
return left.add(right);
}
vec3& operator-(vec3 left, const vec3& right) {
return left.subtract(right);
}
vec3& operator*(vec3 left, const vec3& right) {
return left.multiply(right);
}
vec3& operator/(vec3 left, const vec3& right) {
return left.divide(right);
}
vec3& vec3::operator+=(const vec3& other) {
*this = *this + other;
return *this;
}
vec3& vec3::operator-=(const vec3& other) {
*this = *this - other;
return *this;
}
vec3& vec3::operator*=(const vec3& other) {
*this = *this * other;
return *this;
}
vec3& vec3::operator/=(const vec3& other) {
*this = *this / other;
return *this;
}
bool vec3::operator==(const vec3& other) {
return x == other.x && y == other.y && z == other.z;
}
bool vec3::operator!=(const vec3& other) {
return !(*this == other);
}
std::ostream& operator<<(std::ostream& stream, const vec3& vector) {
stream << "vec3: (" << vector.x << ", " << vector.y << ", " << vector.z << ")";
return stream;
}
int main() {
vec3 v1(1, 2, 3);
vec3 v2(4, 5, 6);
vec3 v3 = v1 + v2;
std::cout << v3.x << ' ' << v3.y << ' ' << v3.z << '\n';
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Armin Montigny |