Dot product

I'm gonna use a Dotproduct for multiplying two 3D point(a & b).
like this a.dot(b)
Error is:
error: extra qualification 'Vector3D::' on member 'dot' [-fpermissive]|

How can I fix it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>
using namespace std;

class Vector3D {
private:
    double x, y, z;
public:
    Vector3D (double x=0, double y=0, double z=0) : x(x), y(y), z(z) {
    }
    friend ostream& operator << (ostream& s, Vector3D v){
        return s << v.x << "," << v.y << "," << v.z;
    }
    
    double Vector3D::dot (const Vector3D& b){
        return (b.x, b.y, b.z);
    }

};


int main() {
    Vector3D a(0.5, 2.0, 2.0);
    Vector3D b(-2, 1, 0);

    double c = a.dot(b);  // 0.5*-2 + 2.0*1.0 + 2.0* 0.0 = 1.0
    cout << d << '\n';
Last edited on
When you declare a member variable or member function within the class definition you don't need to scope the class with the scope resolution operator. Just remove the "Vector3D::".

It returns 0 !
When you add two numbers, you don't write:
 
int c = a.plus(b);
you write:
 
int c = a + b;

Similarly, you should use a dot product function as:
 
double c = dot(a, b);

So it shouldn't be a member function. It should look like:
1
2
3
4
double dot(const Vector3D& a, const Vector3D& b)
{
    return // do your calculation
}
kbw you right but that's what I'm gonna do!?
Topic archived. No new replies allowed.