我正在尝试在Microsoft Visual Studio 2017中对此代码进行bool
专业化。
BinaryParser.h
:
#pragma once
#include <stdint.h>
#include <array>
#include <vector>
#include <string>
#include "Deserializable.h"
#include "ParserError.h"
class Deserializable;
class BinaryParser
{
public:
BinaryParser(const uint8_t* in, size_t in_len);
size_t GetRemaining();
template <class T>
T Get(bool check=true)
{
static_assert(std::is_base_of<Deserializable, T>::value, "Type does not implement Deserializable.");
T obj = T();
obj.Deserialize(this, check);
return obj;
}
template <>
bool Get(bool check);
private:
const uint8_t* data;
size_t size;
size_t index = 0;
};
BinaryParser.cpp
:
#include "stdafx.h"
#include "BinaryParser.h"
BinaryParser::BinaryParser(const uint8_t* in, size_t in_len)
{
data = in;
size = in_len;
}
size_t BinaryParser::GetRemaining()
{
return size - index;
}
template <>
bool BinaryParser::Get(bool check)
{
if (check && GetRemaining() < sizeof(bool)) throw ParserError();
uint8_t r = data[index];
index += sizeof(bool);
return r;
}
不幸的是,每当我这样做:
BinaryParser p(...);
bool a = p.Get<bool>();
我收到此错误:
LNK2019 unresolved external symbol "public: bool __thiscall BinaryParser::Get<bool>(bool)" (??$Get@_N@BinaryParser@@QAE_N_N@Z) referenced in function _wWinMain@16
在过去的2个小时里,我一直把头撞在墙上,试图弄清楚这一点。我有一个针对uint8_t
的模板专业化版本,并且进行了编译,但是bool
,uint16_t
和uint32_t
给了我这个错误(对于{{1 }})。
如果我注释掉unsigned int __thiscall BinaryParser::Get<unsigned int>(bool)
,但保留声明和定义,也不会出现任何错误。