English 中文(简体)
静态班级成员获得“未明确参考”。 Don t 知道为什么[重复]
原标题:static class member gets "undefined reference". Don t know why [duplicate]

我不知道这一法典有什么错误。 我的班级非常简单:

class SetOfCuts{
public:

  static LeptonCuts   Leptons;
  static ElectronCuts TightElectrons;
  static ElectronCuts LooseElectrons;
  //***
  //more code
};

并且,例如,电离层在同一个档案中被定义为:

struct ElectronCuts{
  bool Examine;
  //****
  //other irrelevant stuff
};

我认为,没有什么太复杂的事情。

我的理解是,在主要方案中,我可以做到:

SetOfCuts::LooseElectrons.Examine = true;

但是,如果我这样做的话,我可以:

 undefined reference to `SetOfCuts::LooseElectrons 

如果是,我确实:

 bool SetOfCuts::LooseElectrons.Examine = true;

我收到了:

error: expected initializer before  .  token

我不知道我为什么不能接触这些结构的成员。 我缺少一些关于静态数据成员的明显信息,但我不知道什么。

Thanks a lot.

最佳回答

任何静态提及也必须在具体来源档案(而不仅仅是在标题档案中)中申报,因为当联系时,必须存在某些地方。

例如,请在您的<代码>中说明这一点。 Foo.h

class SetOfCuts{
public:

  static LeptonCuts   Leptons;
  static ElectronCuts TightElectrons;
  static ElectronCuts LooseElectrons;
};

然后在上填写。 页: 1

#include <Foo.h>
LeptonCuts SetOfCuts::Leptons = whatever;
ElectronCuts SetOfCuts::ThighElectrons = whatever;
..

最后,在您的领导下。 相信你能够这样做

#include <Foo.h>
SetOfCuts::Leptons = whatever;
问题回答

您重新获得的“未界定的参考”错误是一种联系错误,表明你已宣布<><>。 静态数据成员,但实际数字为下限/>。 在C++中,有两个步骤使用静态变量——你首先在舱内具体说明,正如你已经做的那样,然后不得不在某个地方对其作出定义。 这类似于你如何界定负责人的职能——你将领导职能原型,然后在来源档案中提供执行。

In your case, in the source file in which you ve implemented the member functions for SetOfCuts, add the following lines:

LeptonCuts SetOfCuts::Leptons;
ElectronCuts SetOfCuts::TightElectrons;
ElectronCuts SetOfCuts:LooseElectrons;

这在实际定义的翻译单位中指C++。 如果你愿意,你也可以在此具体提出论据。 请注意:not 这里的关键词。

希望这一帮助!





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签