C++ stringstream导致链接错误?
我有以下课程:C++ stringstream导致链接错误?,c++,std,C++,Std,我有以下课程: #ifndef CGE_NET_MESSAGE_PARSER_HPP #define CGE_NET_MESSAGE_PARSER_HPP #include "Game/platform.hpp" #include <vector> #include <string> #include <sstream> namespace cge { class NetMessageParser { static std::
#ifndef CGE_NET_MESSAGE_PARSER_HPP
#define CGE_NET_MESSAGE_PARSER_HPP
#include "Game/platform.hpp"
#include <vector>
#include <string>
#include <sstream>
namespace cge
{
class NetMessageParser
{
static std::stringstream ss;
static void clearStream();
public:
NetMessageParser(void);
static int parseInt(const std::string &str);
static float parseFloat(const std::string &str);
static double parseDouble(const std::string &str);
static std::vector<int> parseIntVectorString(
std::string str, char startKey, char endKey, char separator);
static std::string numberToString(int n);
static std::string numberToString(float n);
static std::string numberToString(double n);
virtual ~NetMessageParser(void);
};
}
#endif
这将产生以下链接器错误:
错误3错误LNK2001:未解析的外部符号专用:静态
类标准::基本字符串流,类
std::allocator>cge::NetMessageParser::ss
?ss@NetMessageParser@cge@@0V?$basic_stringstream@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@A NetMessageParser.obj
有什么问题吗?您还必须在类之外定义静态成员,否则它们将被视为外部成员。添加以下内容:
static std::stringstream NetMessageParser::ss;
在您的类之外,链接器错误应该消失。您必须在CPP文件中声明静态变量的存储。你可能想要这样的东西 std::stringstream NetMessageParser::ss 哦,不要把它放在头文件中,否则您将得到关于多个定义的错误这与std::stringstream无关。您无法完全阅读C++书中关于如何定义静态成员的章节。关于这一点,有一亿多亿个问题。