简体   繁体   English

静态类模板实例作为成员导致未解决的外部符号错误

[英]Static class template instance as member causes unresolved external symbol error

I have a template class for a stack (on which I can perform push/pop operations). 我有一个用于堆栈的模板类(可以在其中执行推入/弹出操作)。

If I call the doSomething() function via 如果我通过调用doSomething()函数

A::doSomething();

I get an "unresolved external symbol..." error message. 我收到“无法解析的外部符号...”错误消息。 How can I create an static stack in my class a on which I can perform push and pop operations? 如何在我的类中创建可以执行推入和弹出操作的静态堆栈?

class A {
    private:
        // stack which can hold 4 integers
        static stack<int, 4> s;

    public:
        static void doSomething() {
            s.push(4);
        }
};

You can see a code snippet here: codeshare.io/arJmmY 您可以在此处查看代码段:codeshare.io/arJmmY

With

class A {
   private:
      static stack<int, 4> s;

    // ...
};

you declare the static member s of the class A . 声明静态成员s类的A

You have also to define it. 您还必须定义它。

You have to add 您必须添加

stack<int, 4> A::s;

after the A body. A体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于“未解析的外部符号”,使用 static class 功能会导致 linker 错误,但我不知道如何修复它? - Using a static class fuction causes a linker error thanks to an "unresolved external symbol" but I have no idea how to fix it? 使用静态 constexpr 成员无法解析的外部符号 - Unresolved external symbol using static constexpr member 错误 LNK2001:未解析的外部符号“私有:静态类 - error LNK2001: unresolved external symbol "private: static class 错误:LNK2001:未解析的外部符号“专用:静态类 - Error: LNK2001: unresolved external symbol "private: static class 静态类成员上未解析的外部符号 - Unresolved external symbol on static class members 错误 LNK2001:未解析的外部符号(无法加载 SFML 库的每个 static 成员) - error LNK2001: unresolved external symbol (every static member of SFML library can't be loaded) LNK 2001:初始化静态成员时未解析的外部符号 - LNK 2001 : unresolved external symbol while initializing static member 非法引用非静态成员或未解析的外部符号 - illegal reference to non-static member or unresolved external symbol 定义类时出现未解决的外部符号错误 - unresolved external symbol error when define the class 类的未解析的外部符号? - Unresolved External Symbol for Class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM