静态类成员上未解析的外部符号 [英] Unresolved external symbol on static class members

查看:150
本文介绍了静态类成员上未解析的外部符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常简单的:

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

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

无论如何,我已经定义了两个静态unsigned字符在我的类的公共范围,当我试图修改这些值在同一个类的构造函数,我在编译时出现未解析的外部符号错误。

Anyway, I have defined two static unsigned chars 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;
}

我是C ++的新手,为什么我不能这样做?

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

推荐答案

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

You forgot to 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;

并且,在定义中(通常在CXX文件中)它通常在.H文件中)

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

这篇关于静态类成员上未解析的外部符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆