在c ++中使用静态类 [英] Using static class in c++
本文介绍了在c ++中使用静态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在C ++中有一些简单的假设静态类:
I have some simple hypothetical static class in C++:
#ifndef __STAT_H_
#define __STAT_H_
class Stat {
private:
static vector<int> v;
public:
static void add_num(int num);
static void clear_nums();
static void get_count();
};
#endif
并且ccp文件是这样:
And the ccp file is so:
#include "Stat.h"
vector<int> v;
void Stat::add_num(int num) {
v.push_back(num);
}
void Stat::clear_nums() {
v.clear();
}
int Stat::get_num_count() {
return v.size();
}
现在当我在main.cpp文件Stat.h使用一些静态方法:
Now when I include in main.cpp file "Stat.h" and try to use some static method:
Stat::add_num(8);
编译期间的错误是
未定义引用'Stat :: add_num(int)'
undefined reference to 'Stat::add_num(int)'
在这种情况下可能有什么问题?谢谢。
What can be the problem in this case? Thank you.
编辑:对不起地址向量,应该是那里
sorry about addresses vector, it should be v there
推荐答案
听起来你在编译中没有包含stat.cpp。所以你的链接器找不到静态方法的实现。
It sounds like you have not included stat.cpp in compilation. So your linker cannot find an implementation for the static methods.
这篇关于在c ++中使用静态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文