c++中引入全局static下,产生的问题

      第一次引入static,开始将静态变量的声明和定义写在头文件中,报如下错:

     1>xxx.obj : error LNK2005: "public: static int xxx" (?g_testRound@Res@@2HA) 已经在 xxx.obj 中定义

     1>E:\workspace\project\cpp\xxx\Debug\xxx.exe : fatal error LNK1169: 找到一个或多个多重定义的符号


      百思不得其解,跟度娘,亲密了好长一段落时间,没有找到头绪,报着试试看的态度,将静态变量的声明和定义分开。让定义的工作放在.cpp文件中,问题得以解决,示例如下:         

class Res
{
public:
	static int g_filterAckCount; // add filter后接收到的ack个数
	static int g_resetAckCount; // reset后接收到的ack个数
	static int g_testRound; // 当前正在测试第几轮


	Res();
	~Res();
};

#include "res.h"

int Res::g_filterAckCount = 0; // add filter后接收到的ack个数
int Res::g_resetAckCount = 0; // reset后接收到的ack个数
int Res::g_testRound = 0; // 当前正在测试第几轮

Res::Res()
{
}


Res::~Res()
{
}

           ok,问题得以解决!特此记录!



参考链接:https://www.cnblogs.com/MuyouSome/p/3332699.html

猜你喜欢

转载自blog.csdn.net/n_fly/article/details/78690514