访问 C++ 中成员对象的方法;
我试图从包含以下实例的另一个类的对象 (myPlanet
) 的方法访问对象 (myEOS.calc(...)
) 的方法第一个类 (static EOS myEOS
):
// class in class
class EOS {
public:
static float Y;
float calc(float);
};
float EOS::calc(float x){
return x; // some complicated calculation
}
class Planet {
public:
static EOS myEOS; // want only one instance; cf. below
static void setX(float* X); // must be static; cf. below
};
void Planet::setX(float* X) {
*X = myEOS.calc(*X); // will give an error
}
int main(){
Planet myPlanet;
}
这将返回链接时错误
In function `Planet::setX(float*)':
test.cpp:(.text+0x1a): undefined reference to `Planet::myEOS'
collect2: ld returned 1 exit status
使用 -c
分别编译类和主程序(使用 # include
在主文件中)没有给出错误;这看起来像是解决问题的钥匙,但我没有看到锁!
有人知道问题是什么吗?我希望我的意图从我所掌握的内容中是清楚的,即使存在一些根本性的误解。我以为我在某种程度上理解了类并重读了教程,但没有看到类内类的讨论(不是嵌套类)。我也无法在此网站上找到类似的问题(我通常可以从中得到我需要的所有答案!)。
顺便说一下,按照别人的问题 ,添加显式构造函数(并在 Planet 的初始化列表中正确初始化 EOS)并没有消除编译器关于“对 Planet::myEOS' 的未定义引用”的抱怨(这是在没有
static 的情况下完成的)关键字)。
最后,请注意,Planet::setX
需要是静态的,因为指向此方法的指针必须具有“无类”签名,因为它被传递给无法处理方法/类的函数:
void (*fun_ptr)(float*) = & (Planet::setX);
这也是强制对象 myEOS
为静态(需要从静态函数访问),无论如何,EOS
对象的初始化是昂贵的。
非常感谢任何人的帮助!
I am trying to access a method of an object (myEOS.calc(...)
) from a method of an object (myPlanet
) of another class containing an instance of the first class (static EOS myEOS
):
// class in class
class EOS {
public:
static float Y;
float calc(float);
};
float EOS::calc(float x){
return x; // some complicated calculation
}
class Planet {
public:
static EOS myEOS; // want only one instance; cf. below
static void setX(float* X); // must be static; cf. below
};
void Planet::setX(float* X) {
*X = myEOS.calc(*X); // will give an error
}
int main(){
Planet myPlanet;
}
This returns the linking-time error
In function `Planet::setX(float*)':
test.cpp:(.text+0x1a): undefined reference to `Planet::myEOS'
collect2: ld returned 1 exit status
Compiling separately, with -c
, the classes and the main program (with an #include
in the main file) gives no error; this looks like the key to the solution but I do not see the lock!
Does someone know what the problem is? I hope my intent is clear from what I have, even if there is some fundamental misconception. I thought I somewhat understood classes and reread tutorials but saw no discussion of classes within classes (not nested classes). I was not able to find a similar question on this site (from which I can usually get all the answers I need!) either.
By the way, following somebody else's question, adding explicit constructors (and correctly initialising the EOS in Planet's initialiser list) did not remove the compiler complaint about "undefined reference to Planet::myEOS'" (this done without the
static` keyword).
Finally, note that Planet::setX
needs to be static because a pointer to this method must have a "class-less" signature as it is passed to a function that cannot handle methods/classes:
void (*fun_ptr)(float*) = & (Planet::setX);
This also forces the object myEOS
to be static (needs to be accessed from a static function), and anyway the initialisation of EOS
objects is expensive.
Thanks a lot for anybody's help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这段代码表示“编译器:稍后的某个地方将成为此类的全局
EOS myEOS
。编译器说“ok”,并且它是东西,等待你告诉它全局EOS myEOS
在哪里。然后链接器来清理编译器的混乱,并说“我找不到
myEOS
,你把myEOS
放在哪里了?”并显示错误。您需要将以下行添加到 CPP 文件中的某处:
This code says "Compiler: Somewhere later will be a global
EOS myEOS
for this class. And the compiler says "ok", and does it's thing, waiting for you to tell it where the globalEOS myEOS
is.Then the linker comes along to clean up the compiler's mess, and says "I can't find the
myEOS
, where'd you place themyEOS
?" And displays an error.You need to add the following lines to a CPP file somewhere:
定义静态变量。
请参阅标准 (98) 中的 9.4.2 静态数据成员。
Define the static variable.
See 9.4.2 Static Data Members in the standard (98).