not creating an object properly?

I'm trying to make a vector3D class for a physics simulation and there is a strange error for some of the operator functions:

1
2
3
4
5
vector3D.cpp: In member function 'Vector3D Vector3D::operator+(const Vector3D&)':
vector3D.cpp:28:10: error: request for member 'x' in 'temp', which is of non-class type 'Vector3D()'
vector3D.cpp:29:10: error: request for member 'y' in 'temp', which is of non-class type 'Vector3D()'
vector3D.cpp:30:10: error: request for member 'z' in 'temp', which is of non-class type 'Vector3D()'
vector3D.cpp:32:12: error: conversion from 'Vector3D (*)()' to non-scalar type 'Vector3D' requested


Here is the declaration and appropriate function:

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef VECTOR3D
#define VECTOR3D

// Can be used as a 2D vector
class Vector3D
{
public:
    
    // Dimensions of the vector
    double x;
    double y;
    double z;

    // Constructors
    Vector3D (double x = 0, double y = 0, double z = 0);

    Vector3D (const Vector3D &);
    
    // Vector operations
    Vector3D operator + (const Vector3D &);
    Vector3D operator - (const Vector3D &);

    Vector3D & operator += (const Vector3D &);
    Vector3D & operator -= (const Vector3D &);

    Vector3D operator * (double);
    Vector3D operator / (double);

    Vector3D & operator *= (double);
    Vector3D & operator /= (double);
    
    bool operator == (const Vector3D &);
    bool operator != (const Vector3D &);
    
    // Returns the magnitude of the vector
    double getMagnitude ();

    // Returns the vector scaled down to a unit sphere
    Vector3D scaleToUnit ();
        
};

#endif 


1
2
3
4
5
6
7
8
9
10
Vector3D Vector3D::operator + (const Vector3D &param)
{
    Vector3D temp ();
    
    temp.x = x + param.x;
    temp.y = y + param.y;
    temp.z = z + param.z;

    return temp;
}


There are also seemingly identical errors for the -, *, and / operators.
You could try:
1
2
3
4
5
6
7
8
9
10
11
12
Vector3D Vector3D::operator + (const Vector3D &param)
{
    // Vector3D temp (); // compiler may think this is function declartion

    Vector3D temp; // no braces when no parameters
    
    temp.x = x + param.x;
    temp.y = y + param.y;
    temp.z = z + param.z;

    return temp;
}
Last edited on
That worked.

Thanks!
From Scott Myers on return value optimization for objects, you can also try

 
return Vector3D(x + param.x,y + param.y,z + param.z);
Topic archived. No new replies allowed.