[英]I get a “nonstatic member reference must be relative to specific object” error in a DLL project C++
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: 我正在处理DLL项目,正在编写一个类,其标题中包含变量和函数,并在.cpp文件中定义如下:
.h: 。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: 和.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. 好的,所以在构造函数中我没有问题,但是在
getPoint()
函数中,我收到“非静态成员引用必须相对于特定对象”错误,并且它不会让我使用变量。 I tried making the variables static, but that gives me unresolved external symbol errors in the same places, in getPoint(). 我尝试将变量设为静态,但这在getPoint()中的相同位置给了我未解决的外部符号错误。 What should I do to fix this?
我应该怎么做才能解决这个问题?
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. 您无法从
Point::getPoint
访问X
, Y
, Z
和alpha
,因为getPoint
函数是静态的。 A static member function cannot access instance data members but it can access static
class members. 静态成员函数无法访问实例数据成员,但可以访问
static
类成员。
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 (eg, double Point::X;
). 您不能仅通过添加
static
关键字就使成员成为静态成员,还需要定义它们(例如, double Point::X;
)。
What should I do to fix this?
我应该怎么做才能解决这个问题?
Make the getPoint
function non-static and updated it to use references. 使
getPoint
函数成为非静态的,并对其进行更新以使用引用。
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 (ie, they are copies of the originals) which is modifying temporary variables that only exist within the scope of the getPoint
function. 如果您不使用参数引用,则函数完成后更改将丢失,因为参数是按值传递的(即,它们是原始副本),这将修改仅存在于
getPoint
范围内的临时变量功能。
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
. 较小的问题是
getPoint
函数是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 (ie static
data). 因此,它不能访问属于实例的数据成员,因为允许类函数仅访问属于整个类的数据(即
static
数据)。 You can fix this problem by making your getPoint
function non-static: 您可以通过使
getPoint
函数为非静态来解决此问题:
void getPoint(double x, double y, double z, float a);
The bigger problem is that when you make these assignments inside getPoint
更大的问题是,当您在
getPoint
进行这些分配时
x = X;
y = Y;
z = Z;
a = alpha;
you probably expect them to have some effect in the caller of your getPoint
. 您可能希望它们会对
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. 您对这些参数所做的任何修改都是在
getPoint
本地进行的,并且对传递给函数的表达式没有影响。
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 (eg variables or data members of other classes) but it lets the function modify the variables passed in. 这将参数表达式限制为可写的东西(例如变量或其他类的数据成员),但它允许函数修改传入的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.