编辑*:完整的错误消息:test_huffman.cpp:在成员函数'virtual void HuffmanTest_Test_Test :: TestBody()'
我正在尝试为某些cpp代码编写google测试。我收到错误,我不确定如何修复它。这是错误:test_huffman.cpp:在成员函数'virtual void HuffmanTest_Test_Test :: TestBody()'中。我在网上找到的唯一答案是我需要一个参数列表?不知道从哪里开始。 huffman.h / .cpp可能没有必要,但我想我会分享它们(不,我没有写它们是开源的);
以下是编译时遇到错误的代码(w / gcc):
#include "gtest/gtest.h"
#include "huffman.h"
TEST(HuffmanTest, Test)
{
char* words[3];
words[0] = "one\0";
words[1] = "two\0";
words[2] = "three\0";
int frequencies[UniqueSymbols] = {0};
for(int i = 1; i < 3; i++)
{
const char* ptr = words[i];
while (*ptr != '\0')
++frequencies[*ptr++];
}
INode* root = BuildTree(frequencies);
HuffCodeMap codes;
GenerateCodes(root, HuffCode(), codes);
delete root;
EXPECT_FALSE(codes.empty());
}
Huffman.h(开源):
#ifndef huffman_h_INCLUDED
#define huffman_h_INCLUDED
#include <iostream>
#include <queue>
#include <map>
#include <climits> // for CHAR_BIT
#include <iterator>
#include <algorithm>
const int UniqueSymbols = 1 << CHAR_BIT;
typedef std::vector<bool> HuffCode;
typedef std::map<char, HuffCode> HuffCodeMap;
class INode
{
public:
const int f;
virtual ~INode();
protected:
INode(int f);
};
class InternalNode : public INode
{
public:
INode *const left;
INode *const right;
InternalNode(INode* c0, INode* c1);
~InternalNode();
};
class LeafNode : public INode
{
public:
const char c;
LeafNode(int f, char c);
};
struct NodeCmp
{
bool operator()(const INode* lhs, const INode* rhs) const;
};
INode* BuildTree(const int (&frequencies)[UniqueSymbols]);
void GenerateCodes(const INode* node, const HuffCode& prefix, HuffCodeMap& outCodes);
#endif // huffman_h_INCLUDED
huffman.cpp(开源):
#include "huffman.h"
INode :: ~INode()
{
}
INode :: INode(int f)
: f(f)
{
}
InternalNode :: InternalNode(INode* c0, INode* c1)
: INode(c0->f + c1->f), left(c0), right(c1)
{
}
InternalNode :: ~InternalNode()
{
delete left;
delete right;
}
LeafNode :: LeafNode(int f, char c)
: INode(f), c(c)
{
}
bool NodeCmp :: operator()(const INode* lhs, const INode* rhs) const { return lhs->f > rhs->f; }
INode* BuildTree(const int (&frequencies)[UniqueSymbols])
{
std::priority_queue<INode*, std::vector<INode*>, NodeCmp> trees;
for (int i = 0; i < UniqueSymbols; ++i)
{
if(frequencies[i] != 0)
trees.push(new LeafNode(frequencies[i], (char)i));
}
while (trees.size() > 1)
{
INode* childR = trees.top();
trees.pop();
INode* childL = trees.top();
trees.pop();
INode* parent = new InternalNode(childR, childL);
trees.push(parent);
}
return trees.top();
}
void GenerateCodes(const INode* node, const HuffCode& prefix, HuffCodeMap& outCodes)
{
if (const LeafNode* lf = dynamic_cast<const LeafNode*>(node))
{
outCodes[lf->c] = prefix;
}
else if (const InternalNode* in = dynamic_cast<const InternalNode*>(node))
{
HuffCode leftPrefix = prefix;
leftPrefix.push_back(false);
GenerateCodes(in->left, leftPrefix, outCodes);
HuffCode rightPrefix = prefix;
rightPrefix.push_back(true);
GenerateCodes(in->right, rightPrefix, outCodes);
}
}