简体   繁体   English

C ++模板类:实例化错误

[英]C++ template class: Instantiation error

I am doing a project using template classes. 我正在使用模板类做一个项目。 I am almost finish. 我差不多完成了。 However, I am getting warnings and errors about instantiation when I compile my program. 但是,当我编译程序时,我收到有关实例化的警告和错误。

In file included from /usr/include/c++/4.7/utility:72:0,
             from test.cpp:2:
/usr/include/c++/4.7/bits/stl_pair.h: In instantiation of ‘std::pair<_T1, _T2>&  
std::pair<_T1, _T2>::operator=(std::pair<_U1, _U2>&&) [with _U1 = int; _U2 = char; _T1 = const int; _T2 = char; std::pair<_T1, _T2> = std::pair<const int, char>]’:
map1.hpp:30:5:   required from ‘cs540::Node<Key, Value>::Node(std::pair<const Key, 
Value>, cs540::Node<Key, Value>*, cs540::Node<Key, Value>*) [with Key = int; Value = char; cs540::Node<Key, Value> = cs540::Node<int, char>]’
map1.hpp:121:7:   required from ‘void cs540::Map<Key, Value>::insert_node(cs540::Node<Key, Value>*, std::pair<const Key, Value>) [with Key = int; Value = char]’
map1.hpp:433:5:   required from ‘cs540::Map<Key, Value>::Iterator cs540::Map<Key, Value>::insert(const std::pair<const Key, Value>&) [with Key = int; Value = char]’
test.cpp:12:23:   required from here

I feel as though Key and Value are already instantiate once I create an object of that class. 我觉得,一旦我创建了该类的对象,Key和Value就已经实例化了。 I created a custom Map class that is suppose to have the functionality of std::map. 我创建了一个自定义Map类,假设它具有std :: map的功能。 I have all of my classes under a cs540 namespace. 我的所有类都在cs540命名空间下。

Here is my Node class: 这是我的Node类:

template<class Key, class Value> class Node{
    /*Class member variables*/
    private:
        std::pair<const Key,Value> data;
    public: 
        Node<Key,Value>* rightChild;
        Node<Key,Value>* leftChild;
        Node<Key,Value>* next;
        Node<Key,Value>* previous;
        Node() :rightChild(nullptr),
            leftChild(nullptr),
            next(nullptr),
            previous(nullptr){/*Default constructor*/ }
        Node(std::pair<const Key,Value> dataIn, Node* rightIn, Node* leftIn):rightChild(rightIn),leftChild(leftIn){
            data = std::make_pair(dataIn.first, dataIn.second);     
        }       
        ~Node(){/*Destructor*/ }
        Key getKey(){ return data.first;}
        Value getValue(){ return data.second;}
        void setData(const Key key, Value value){ data = std::make_pair(key,value); }
        std::pair<const Key,Value> getData(){ return data;}
};

And the functions that call the Node constructor 以及调用Node构造函数的函数

void insert_node(Node<Key,Value>* current, const std::pair<const Key,Value> newPair){

                if(current == nullptr){
                    num_size++;
                    current = new Node<Key,Value>(newPair,nullptr,nullptr);
                    return;
                }

                if(current->getKey() > newPair.first){
                    if(current->leftChild == nullptr){
                        num_size++;
                        current->leftChild = new Node<Key,Value>(newPair,nullptr,nullptr);
                        return;
                    }else{ insert_node(current->leftChild,newPair);}
                }else if(current->getKey() < newPair.first){
                    if(current->rightChild == nullptr){
                        num_size++;
                        current->rightChild = new Node<Key,Value>(newPair,nullptr,nullptr);
                        return;
                    }else{ insert_node(current->rightChild,newPair);}
                }else{  return;}
            }

Last but not least, my main function 最后但并非最不重要的,我的主要功能

int main(void){

   cs540::Map<int,char> mapper;
   mapper.insert({1,'g'});
   return 0;
}

You try to assign to data which has the first element const, so you cannot modify it. 您尝试分配具有第一个元素const的数据,因此您无法对其进行修改。 Instead of assigning in the constructor body use initialisation list in the same way you used for rightChild and leftChild: 而不是在构造函数体中分配使用初始化列表,就像使用rightChild和leftChild一样:

data(dataIn.first, dataIn.second)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM