问题
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?
回答1:
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.
回答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.
来源:https://stackoverflow.com/questions/32145335/i-get-a-nonstatic-member-reference-must-be-relative-to-specific-object-error-i