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

发布于 2024-07-06 17:54:17 字数 383 浏览 10 评论 0原文

非常简单地说:

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

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

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

    ...

    test();
};

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

旧街凉风 2024-07-13 17:54:17

如果您使用的是C++ 17,则只需使用inline说明符即可(请参阅https ://stackoverflow.com/a/11711082/55721


如果使用旧版本的 C++ 标准,则必须添加定义以匹配

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

某处的 X 和 Y 声明。 您可能还想初始化静态成员,

unsigned char test::X = 4;

并且再次在定义中(通常在 CXX 文件中)而不是在声明中(通常在 .H 文件中)执行此操作

If you are using C++ 17 you can just use the inline specifier (see https://stackoverflow.com/a/11711082/55721)


If using older versions of the C++ standard, you must add the definitions to match your declarations of X and 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)

小鸟爱天空丶 2024-07-13 17:54:17

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

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

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

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

test::test() {
}

如果您希望能够修改静态变量(换句话说,当声明不合适时)它们作为 const),您可以通过以下方式将代码分隔在 .H.CPP 之间:

.H :

class test {
public:

  static unsigned char X;
  static unsigned char Y;

  ...

  test();
};

.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.
}

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

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

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 .H and .CPP in the following way:

.H :

class test {
public:

  static unsigned char X;
  static unsigned char Y;

  ...

  test();
};

.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.
}
我偏爱纯白色 2024-07-13 17:54:17

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

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

在 myClass.cpp 中一样,我尝试使用此 m_nMyVar。 它有 LINK 错误,例如:

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
}

所以我在 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
}

然后 LNK2001 就消失了。

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

//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:

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

//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
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.

不语却知心 2024-07-13 17:54:17

当我们在类中声明静态变量时,该变量被该类的所有对象共享。 由于静态变量仅在它们从未被构造函数初始化时才被初始化。 相反,静态变量应该仅使用作用域解析运算符 (::) 在类外部显式初始化一次。

在下面的示例中,静态变量 counter 是 Demo 类的成员。 请注意它是如何在类外部显式初始化的,初始值 = 0。

#include <iostream>
#include <string>
using namespace std;
class Demo{
   int var;
   static int counter;

   public:
   Demo(int var):var(var){
      cout<<"Counter = "<<counter<<endl;
      counter++;
   }
};
int Demo::counter = 0;                 //static variable initialisation
int main()
{
   Demo d(2), d1(10),d3(1);
}

Output:
Count = 0
Count = 1
Count = 2

When we declare a static variable in a class, it is shared by all the objects of that class. As static variables are initialized only once they are never initialized by a constructor. Instead, the static variable should be explicitly initialized outside the class only once using the scope resolution operator (::).

In the below example, static variable counter is a member of the class Demo. Note how it is initialized explicitly outside the class with the initial value = 0.

#include <iostream>
#include <string>
using namespace std;
class Demo{
   int var;
   static int counter;

   public:
   Demo(int var):var(var){
      cout<<"Counter = "<<counter<<endl;
      counter++;
   }
};
int Demo::counter = 0;                 //static variable initialisation
int main()
{
   Demo d(2), d1(10),d3(1);
}

Output:
Count = 0
Count = 1
Count = 2
独夜无伴 2024-07-13 17:54:17

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

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

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:

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.

倾城花音 2024-07-13 17:54:17

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

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.]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文