C++ 静态类成员上未解析的外部符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/195207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:36:28  来源:igfitidea点击:

Unresolved external symbol on static class members

c++classstaticmembers

提问by Colin Jensen

Very simply put:

非常简单地说:

I have a class that consists mostly of static public members, so I can group similar functions together that still have to be called from other classes/functions.

我有一个主要由静态公共成员组成的类,因此我可以将仍然必须从其他类/函数调用的类似函数组合在一起。

Anyway, I have defined two static unsigned char variables in my class public scope, when I try to modify these values in the same class' constructor, I am getting an "unresolved external symbol" error at compilation.

无论如何,我在我的类公共范围中定义了两个静态无符号字符变量,当我尝试在同一个类的构造函数中修改这些值时,我在编译时收到“未解析的外部符号”错误。

class test 
{
public:
    static unsigned char X;
    static unsigned char Y;

    ...

    test();
};

test::test() 
{
    X = 1;
    Y = 2;
}

I'm new to C++ so go easy on me. Why can't I do this?

我是 C++ 新手,所以放轻松。为什么我不能这样做?

回答by Colin Jensen

You forgot to add the definitions to match your declarations of X and Y

您忘记添加定义以匹配 X 和 Y 的声明

unsigned char test::X;
unsigned char test::Y;

somewhere. You might want to also initialize a static member

某处。您可能还想初始化一个静态成员

unsigned char test::X = 4;

and again, you do that in the definition (usually in a CXX file) not in the declaration (which is often in a .H file)

再一次,你在定义中(通常在 CXX 文件中)而不是在声明中(通常在 .H 文件中)

回答by sergtk

Static data members declarations in the class declaration are not definition of them. To define them you should do this in the .CPPfile to avoid duplicated symbols.

类声明中的静态数据成员声明不是它们的定义。要定义它们,您应该在.CPP文件中执行此操作以避免重复符号。

The only data you can declare and define is integral static constants. (Values of enumscan be used as constant values as well)

您可以声明和定义的唯一数据是整型静态常量。(的值enums也可以用作常量值)

You might want to rewrite your code as:

您可能希望将代码重写为:

class test {
public:
  const static unsigned char X = 1;
  const static unsigned char Y = 2;
  ...
  test();
};

test::test() {
}

If you want to have ability to modify you static variables (in other words when it is inappropriate to declare them as const), you can separate you code between .Hand .CPPin the following way:

如果你想有修改你静态变量的能力(换句话说,当它是不恰当的将它们声明为const),可以单独你们之间的代码.H,并.CPP以下列方式:

.H :

。H :

class test {
public:

  static unsigned char X;
  static unsigned char Y;

  ...

  test();
};

.CPP :

.CPP :

unsigned char test::X = 1;
unsigned char test::Y = 2;

test::test()
{
  // constructor is empty.
  // We don't initialize static data member here, 
  // because static data initialization will happen on every constructor call.
}

回答by Johann Studanski

Since this is the first SO thread that seemed to come up for me when searching for "unresolved externals with static const members" in general, I'll leave another hint to solve one problem with unresolved externals here:

由于这是我在搜索“具有静态常量成员的未解析外部对象”时似乎出现的第一个 SO 线程,因此我将在此处留下另一个提示来解决未解析外部对象的一个​​问题:

For me, the thing that I forgot was to mark my class definition __declspec(dllexport), and when called from another class (outside that class's dll's boundaries), I of course got the my unresolved external error.
Still, easy to forget when you're changing an internal helper class to a one accessible from elsewhere, so if you're working in a dynamically linked project, you might as well check that, too.

对我来说,我忘记的是标记我的类定义__declspec(dllexport),当从另一个类(在该类的 dll 边界之外)调用时,我当然得到了我未解决的外部错误。
尽管如此,当您将内部帮助类更改为可从其他地方访问的帮助类时,很容易忘记,因此如果您在动态链接的项目中工作,您也不妨检查一下。

回答by Penny

in my case, I declared one static variable in .h file, like

就我而言,我在 .h 文件中声明了一个静态变量,例如

//myClass.h
class myClass
{
static int m_nMyVar;
static void myFunc();
}

and in myClass.cpp, I tried to use this m_nMyVar. It got LINK error like:

在 myClass.cpp 中,我尝试使用这个 m_nMyVar。它有 LINK 错误,如:

error LNK2001: unresolved external symbol "public: static class... The link error related cpp file looks like:

error LNK2001: unresolved external symbol "public: static class... 与链接错误相关的 cpp 文件如下所示:

//myClass.cpp
void myClass::myFunc()
{
myClass::m_nMyVar = 123; //I tried to use this m_nMyVar here and got link error
}

So I add below code on the top of myClass.cpp

所以我在 myClass.cpp 的顶部添加以下代码

//myClass.cpp
int myClass::m_nMyVar; //it seems redefine m_nMyVar, but it works well
void myClass::myFunc()
{
myClass::m_nMyVar = 123; //I tried to use this m_nMyVar here and got link error
}

then LNK2001 is gone.

那么 LNK2001 就没了。

回答by whats_wrong_here

In my case, I was using wrong linking.
It was managed c++ (cli) but with native exporting. I have added to linker -> input -> assembly link resource the dll of the library from which the function is exported. But native c++ linking requires .lib file to "see" implementations in cpp correctly, so for me helped to add the .lib file to linker -> input -> additional dependencies.
[Usually managed code does not use dll export and import, it uses references, but that was unique situation.]

就我而言,我使用了错误的链接。
它是由 C++ (cli) 管理的,但具有本机导出功能。我已将导出函数的库的 dll 添加到链接器 -> 输入 -> 程序集链接资源。但是本机 C++ 链接需要 .lib 文件才能正确“查看”cpp 中的实现,因此对我而言,帮助将 .lib 文件添加到链接器 -> 输入 -> 附加依赖项。
[通常托管代码不使用 dll 导出和导入,它使用引用,但那是独特的情况。]