【问题标题】:C++ template class: Instantiation errorC++ 模板类:实例化错误
【发布时间】:2014-04-20 01:05:18
【问题描述】:

我正在使用模板类做一个项目。我快完成了。但是,我在编译程序时收到有关实例化的警告和错误。

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

一旦我创建了该类的对象,我觉得 Key 和 Value 已经实例化了。我创建了一个自定义 Map 类,假设它具有 std::map 的功能。 我的所有类都在 cs540 命名空间下。

这是我的节点类:

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;}
};

以及调用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;}
            }

最后一点,我的主要功能

int main(void){

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

【问题讨论】:

  • 你能发布整个错误信息吗?只能看到trace,看不到问题描述。

标签: c++


【解决方案1】:

您尝试分配具有第一个元素 const 的数据,因此您无法修改它。不要在构造函数主体中分配,而是使用与 rightChild 和 leftChild 相同的方式使用初始化列表:

data(dataIn.first, dataIn.second)

【讨论】:

  • 似乎是对的,insert_node 中的 rhs 在键上应该是非常量
猜你喜欢
  • 1970-01-01
  • 2019-11-17
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多