0

I'm working on a DLL project, I'm writing a class with variables and functions in a header and the definitions in a .cpp file like this:

.h:

#ifndef RE_MATH_H
#define RE_MATH_H
#ifdef MATHFUNCSDLL_EXPORTS
#define RE_MATH_API __declspec(dllimport) 
#else
#define RE_MATH_API __declspec(dllexport) 
#endif
#define PI 3.14159265358979323846

namespace RE_Math
{
    class RE_MATH_API Point
    {
        public:
            double X;
            double Y;
            double Z;
            float alpha;

            Point(double x, double y, double z, float a);
            static void getPoint(double x, double y, double z, float a);
    };
}

and the .cpp:

#include <re_math.h>

namespace RE_Math
{
    Point::Point(double x, double y, double z, float a)
    {
        X = x;
        Y = y;
        Z = z;
        alpha = a;
    }

    void Point::getPoint(double x, double y, double z, float a)
    {
        x = X;
        y = Y;
        z = Z;
        a = alpha;
    }
}

OK, so in the constructor I have no problems, but in the getPoint() function I get the "non-static member reference must be relative to specific object" error and it won't let me use the variables. I tried making the variables static, but that gives me unresolved external symbol errors in the same places, in getPoint(). What should I do to fix this?

Reaper9806
  • 133
  • 1
  • 2
  • 8
  • You marked the function `getPoint` static but are trying to access non-static class members. – melak47 Aug 21 '15 at 16:39
  • additionally, you obviously want to copy Point members X, Y, Z, alpha to x, y, z, a, but it wont work as you are sending x,y,z,a by value and not by reference. – Zdeslav Vojkovic Aug 21 '15 at 16:41

2 Answers2

2

it won't let me use the variables

You cannot access X, Y, Z, and alpha from Point::getPoint because the getPoint function is static. A static member function cannot access instance data members but it can access static class members.

I tried making the variables static, but that gives me unresolved external symbol errors

You cannot make the members static by simply adding the static keyword, you need to define them as well (e.g., double Point::X;).

What should I do to fix this?

Make the getPoint function non-static and updated it to use references.

void Point::getPoint(double& x, double& y, double& z, float& a)
{
    x = X;
    y = Y;
    z = Z;
    a = alpha;
}

If you don't use references for the parameters the changes are lost after the function completes because the parameters are passed by-value (i.e., they are copies of the originals) which is modifying temporary variables that only exist within the scope of the getPoint function.

James Adkison
  • 9,412
  • 2
  • 29
  • 43
2

There are two problems with your code - a small technical problem, and a big design problem.

The smaller problem is that your getPoint function is static. Therefore, it cannot access data members that belong to an instance, because class functions are allowed to access only data that belongs to the entire class (i.e. static data). You can fix this problem by making your getPoint function non-static:

void getPoint(double x, double y, double z, float a);

The bigger problem is that when you make these assignments inside getPoint

x = X;
y = Y;
z = Z;
a = alpha;

you probably expect them to have some effect in the caller of your getPoint. However, these are simply assignments to parameters, which are passed by value. Any modification you make to these parameters are local to getPoint, and have no effect on the expressions passed to your function.

You could fix this problem by passing parameters by reference:

void Point::getPointComponents(double& x, double& y, double& z, float& a) {
    x = X;
    y = Y;
    z = Z;
    a = alpha;
}

and using your function like this:

double x, y, z, a;
myPt.getPointComponents(x, y, z, a);

Ampersand & indicates that the corresponding parameter is passed by reference. This restricts parameter expressions to writable things (e.g. variables or data members of other classes) but it lets the function modify the variables passed in.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523