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