我有Node
类,该类正由另外三个类Leaf
,Branch
和Extension
继承。我正在使用boost :: variant来存储以下任何类的变量:
boost::variant<Node, Branch, Extension, Leaf>;
上一行的节点显示错误如下:
变量“节点”不是类型名称
我的班级头文件如下:
节点类标题
#include "alias.hpp"
class Node {
protected:
buffer_t value_;
char node_type_;
public:
Node();
Node(const Node &node);
char GetNodeType();
void SetNodeType(char input);
buffer_t GetValue();
void SetValue(buffer_t input);
bufferarray_t Raw();
buffer_t Serialize();
buffer_t Hash();
};
扩展类标题
#include "node.hpp"
class Extension : public Node {
private:
nibble_t nibble_;
public:
Extension(nibble_t nibble, buffer_t value);
// ~Extension();
static nibble_t EncodeKey(const nibble_t& input);
static nibble_t DecodeKey(const nibble_t& input);
nibble_t GetKey();
void SetKey(nibble_t input);
nibble_t EncodedKey();
bufferarray_t Raw();
};
叶类标题
#include "node.hpp"
class Leaf : public Node {
private:
nibble_t nibbles_;
public:
// Leaf();
// ~Leaf();
Leaf(nibble_t nibbles, buffer_t value);
static nibble_t EncodeKey(const nibble_t& input);
static nibble_t DecodeKey(const nibble_t& input);
nibble_t GetKey();
void SetKey(nibble_t input);
nibble_t EncodedKey();
bufferarray_t Raw();
};
分支类标题
#include <map>
#include "node.hpp"
class Branch : public Node {
private:
bufferarray_t branches_;
public:
// Branch();
// ~Branch();
static Node FromBuffer(const bufferarray_t &input);
void SetBranch(const int loc, const buffer_t &input);
buffer_t GetBranch(const int input);
bufferarray_t GetBranches();
std::map<int, buffer_t> GetChildren();
bufferarray_t Raw();
};
主类定义如下:
#include <boost/variant.hpp>
#include "node.hpp"
#include "leaf.hpp"
#include "branch.hpp"
#include "extension.hpp"
// Some other includes
class Trie {
int main() {
using node_t = boost::variant<Node, Branch, Extension, Leaf>;
// Code to test
return 0;
}
}
buffer_t
和bufferarray_t
是我用using
创建的别名,分别定义了unint64_t
的向量和buffer_t
的向量。两者都在alias.hpp
文件中定义。
错误的可能原因是什么?:
变量“节点”不是类型名称
编辑:根据@molbdnilo的评论,我认为这是VS代码的怪异行为之一,而不是我的代码。稍加挖掘,我发现无论我有Node
,我的编辑器都将其视为static NodeType Node
。我不知道它从哪里读取此参考,但它不是我的代码的一部分。
问题出在我的代码中。但是注释中没有提到Node是变量。
我在单独的头文件中定义了一个枚举:nodetype.hpp
enum NodeType {
BLANK_NODE = 0,
BRANCH_NODE = 1,
EXTENSION_NODE = 2,
LEAF_NODE = 3
};
在此枚举的定义中,末尾的分号(;)丢失了。该枚举被导入到一个util文件中,在该util文件中,静态函数应返回Node
对象。编辑以某种方式假设NodeType
和Node
是相关的,并认为它是>
static NodeType Node
这是此错误/冲突的原因。