【问题标题】:How to declare a pointer to a class template pointer with different data types in C++如何在 C++ 中声明指向具有不同数据类型的类模板指针的指针
【发布时间】:2018-05-26 17:53:54
【问题描述】:

我有一个用模板设计的程序来实现霍夫曼树。我的 HuffmanNode 类有两种不同的数据类型。这是类的实现。

#pragma once

template <typename T, typename U> class HuffmanNode 
{
private:
    int frequency;
    T value;
    U code;
    HuffmanNode<T, U>* left, *right;
public:
    HuffmanNode<T, U>() :
        value(' '), frequency(0), left(nullptr), right(nullptr) {}
    HuffmanNode<T, U>(T v, U i) :
        value(v), frequency(i), left(nullptr), right(nullptr) {}
    HuffmanNode<T, U>(HuffmanNode<T, U>* left, HuffmanNode<T, U>* right)
    {
        this->left = left;
        this->right = right;
        frequency = left->getFrequency() + right->getFrequency();
        value = NULL;
    }
};

在我的 HuffTree 类中,我有一个指向 HuffmanNode 指针的指针。我尝试以几种不同的方式声明指向 HuffmanNode 指针的指针,但无论哪种方式,我都会收到错误。

我是这样声明的:

HuffmanNode<T>** storage;   // Error: 'HuffmanNode': too few template arguments

然后我尝试了这两种数据类型:

template <class T, class U> 
HuffmanNode<T, U>** storage;   // Error: 'HuffTree<T>::storage': only static data member templates are allowed

如何声明这样的指针?

编辑:我更新了 HuffTree 类以在 HuffmanNode 指针中包含这两种数据类型,但现在我在包含 HuffTree 类的 HuffmanCode 类中遇到错误。这是 HuffTree 类的更新代码:

#pragma once
#include "HuffmanNode.h"
using namespace std;

template <typename T, typename U> class HuffTree 
{
private:
    HuffmanNode<T, U>** storage;
    int treeSize;
    int capacity;

public:
HuffTree<T, U>(int size)
{
    capacity = size;
    treeSize = 0;
    storage = new HuffmanNode<T, U>*[capacity];
    for (int i = 0; i < capacity; i++)
    {
        storage[i] = NULL;
    }
}
// I get an error in the HuffmanCode class when calling this function
void insert(HuffmanNode<T, U>* rhs)
{
    treeSize++;
    storage[treeSize - 1] = rhs;
    percUp(treeSize - 1);
}
};

这是 HuffmanCode 类的代码,现在我修复了指针问题,它给了我一个错误:

#pragma once
#include <string>
#include <map>
#include"HuffmanTree.h"
using std::string;
using std::map;
template <typename T, typename U> class HuffmanCode 
{
private:
    string data;
    string encodedData;
    HuffTree<T, U>* tree;
    map<T, U> frequencyTable;
    map<T, U> huffmanTable;
    void buildTree()
    {
        for (map<T, U>::iterator it = frequencyTable.begin();
            it != frequencyTable.end(); ++it)
        {
            /* 
            this line gives me an error: 'HuffmanNode<char,std::string>::
            HuffmanNode(const HuffmanNode<char,std::string> &)': 
            cannot convert argument 1 from 'const std::string' to 'char'
            */
            tree->insert(new HuffmanNode<char, string>(it->first, it->second));
        }
    }
public:
    HuffmanCode<T, U>(T input)
    {
        data = input;
        encodedData = "";
        // create tree to be large enough for worst case
        tree = new HuffTree<T, U>(data.length());
        buildTable();
        buildTree();
    }
    };

这是我测试这些功能的源文件:

#include <iostream>
#include <string>
#include "HuffmanCode.h"
using namespace std;

int main()
{
    string text;
    cout << "Enter some text:\n";
    getline(cin, text);
    HuffmanCode<string, string>* huffText = new HuffmanCode<string, string>(text);

    huffText->displayTable();
    huffText->displayHuffmanTable();

    string code = huffText->getEncodedString();
    cout << "Encoded string: " << code << endl << endl;
    cout << "Decoded string: " << huffText->decodeString(code) << endl << endl;

    delete huffText;

    return 0;
}

已编辑以包含相关代码

【问题讨论】:

  • 没有“双指针”这样的东西。你能不使用模板来编写你的树吗?如果没有,请不要尝试使用模板编写。
  • 创建模板很难(起初...Scott Meyers、Herb Sutter、Sean Parent 可能不同意我的观点)。 Neil Butterworth 的建议非常好。首先制作不作为模板的类。一旦你让它们工作,然后从细节中“抽象”出模板。
  • 我编辑了我的帖子,将“双指针”更改为“指针对指针”。我需要使用模板为作业编写树。
  • @Eljay 我做到了 - 我让类在没有模板的情况下工作,这是我需要“抽象”出来的最后一部分。
  • 除非HuffmanNode的模板参数可以从HuffTree的模板参数中确定,否则你想要的没有意义。我们需要更多背景信息。

标签: c++ templates pointer-to-pointer


【解决方案1】:

如果您无法定义其他模板参数类型,请在HuffTree 类中自行指定。其中之一应该适合您:

HuffmanNode&lt;T, T&gt; ** storage;

HuffmanNode&lt;T, string&gt; ** storage;

(或您认为合适的任何其他数据类型)

【讨论】:

  • 我编辑了我的帖子以包含指向我的所有代码的链接。
  • 存储声明是一样的。为了完整性,我添加了“完整”类声明。你可以复制那行。
  • 我试过了,但我遇到了更多错误。 HuffTree 用于另一个类,它也需要类 T 和类 U 作为参数,但这不起作用。我在 github 上发布了完整程序的链接,因此您可以查看我的所有代码。
猜你喜欢
  • 2011-09-14
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-01
  • 1970-01-01
  • 2018-06-28
相关资源
最近更新 更多