访问模板类中的函数

时间:2014-06-23 09:54:11

标签: c++ templates function-call

我正在处理以下代码,并希望从另一个函数访问OutEdgeCount。我尝试初始化Gate门,然后使用gate.OutEdgeCount() 门*门。上述两种初始化都产生了错误。我是C ++的新手。如果有人可以帮助我或指出我可以参考的任何资源,那将是非常有帮助的。

template <typename delay_type, typename error_type, typename power_type, typename   mask_type>
class Gate: public Gate_Type<delay_type, error_type, power_type, mask_type>
{
public:
    vector<delay_type> delay;
    vector<error_type> error_probability;
    power_type power;
    vector<mask_type> mask;

    //Constructor
    void GateCopy(Gate<delay_type, error_type, power_type, mask_type> &gate)
    {
        this->gate_name = gate.gate_name;
        this->inout_port_list = gate.inout_port_list;
        this->in_port_list = gate.in_port_list;
        this->out_port_list = gate.out_port_list;
        this->first_in_edge = NULL;
        this->first_out_edge = NULL;
        this->delay.clear();
        this->error_probability.clear();
        this->power = 0;
        this->mask.clear();
        this->parent_block_index = 0;   //the index in sub block list all
        this->active = gate.active;
    }

    int OutEdgeCount(vector<int> out_degree_vec)
    {
        int count = 0;
        Edge<delay_type, error_type> *p_edge = this->first_out_edge;
        if(p_edge == NULL)
        {
            return 0;
        }
        else
        {
            while(p_edge != NULL)
            {
                if(out_degree_vec.at(p_edge->head_index) != -1)
                {
                    count++;
                }
                p_edge = p_edge->same_tail_next;
            }
            return count;
        }
    }
}
void FindAllPaths()
{
   Gate gate;
   ...
 }

错误如下 -

graph.h:227:6: error: ‘template<class delay_type, class error_type, class power_type, class mask_type> class graph_space::Gate’ used without template parameters
In file included from main.cpp:3:0:
graph.h: In member function ‘void graph_space::Path<delay_type, error_type, power_type, mask_type>::FindAllPaths(graph_space::Graph<delay_type, error_type, power_type, mask_type>&)’:
graph.h:1797:11: error: missing template arguments before ‘gate’
graph.h:1797:11: error: expected ‘;’ before ‘gate’
graph.h:1895:39: error: ‘gate’ was not declared in this scope
graph.h:1930:38: error: ‘gate’ was not declared in this scope

1 个答案:

答案 0 :(得分:0)

要实例化模板类型,必须提供实例的模板参数。例如,当您创建std::map<K, V>时,您必须提供KV的类型,即std::map<std::string, Foo> allTheFoo;

所以对你的例子

Gate<Milliseconds, Exception, Cube, LowerQuadMask> gate;

当然,我刚刚在这里编写了模板类型的名称。

相关问题