stringstream causes link error?
I have the following class:
#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
Which produces the following linker error:
Error 3 error LNK2001: unresolved external symbol "private: static class std::basic_stringstream,class std::allocator > cge::NetMessageParser::ss" (?ss@NetMessageParser@cge@@0V?$basic_stringstream@DU?$char_traits@开发者_运维问答D@std@@V?$allocator@D@2@@std@@A) NetMessageParser.obj
What could be wrong?
You have to define static members outside of the class as well or they will be considered external. add this:
static std::stringstream NetMessageParser::ss;
outside of your class and the linker error should go away.
You have to declare storage for static variables in the CPP file. You probably want something like this
std::stringstream NetMessageParser::ss;
Oh and do not put this in header file otherwise you will get errors about multiple definitions
精彩评论