Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法转换';常数T';至';模板<;T>;功能';_C++ - Fatal编程技术网

C++ 无法转换';常数T';至';模板<;T>;功能';

C++ 无法转换';常数T';至';模板<;T>;功能';,c++,C++,我不明白为什么我的节点构造函数会出现转换错误,节点构造函数接受“const t&”作为第一个参数,这就是我的insert方法传递给构造函数的内容,但出于某种原因,它仍然会向我抛出该错误 错误C2440:“正在初始化”:无法从“常量记录”转换为“SortedList::Node”* SortedList的类型为“Record”,但如果我在中间键入任何内容,括号中的所有内容都将消失。 列表类: class SortedList{ struct Node{ T data_; Node*

我不明白为什么我的节点构造函数会出现转换错误,节点构造函数接受“const t&”作为第一个参数,这就是我的insert方法传递给构造函数的内容,但出于某种原因,它仍然会向我抛出该错误

错误C2440:“正在初始化”:无法从“常量记录”转换为“SortedList::Node”*

SortedList的类型为“Record”,但如果我在中间键入任何内容,括号中的所有内容都将消失。

列表类:

class SortedList{
struct Node{
    T data_;
    Node* next_;
    Node* prev_;
    Node(const T& data=T{},Node* nx=nullptr,Node* pr=nullptr){
        data_ = data;
        next_ = nx;
        prev_ = pr;
    }
};
Node* front_;
Node* back_;
public:
class const_iterator{ 
    friend class SortedList;
  protected:
    Node* curr_;
    const_iterator(Node* n){ 
        curr_ = n; 
    }
  public:
    const_iterator(){
        curr_ = nullptr;
    }
    const_iterator operator++(){
        curr_ = curr_->next_;

        return *this;
    }
    const_iterator operator++(int){ //advances curr returns old
        const_iterator old = *this;
        curr_ = curr_->next_;

        return old;
    }
    const_iterator operator--(){
        curr_ = curr_->prev_;

        return *this;
    }
    const_iterator operator--(int){
        const_iterator temp = *this;
        curr_ = curr_->prev_;

        return temp;
    }
    bool operator==(const_iterator rhs){
        return curr_->data_ == rhs.curr_->data_;
    }
    bool operator!=(const_iterator rhs){
        return curr_->data_ != rhs.curr_->data_;
    }
    bool operator<(const_iterator rhs){
        return curr_->data_ < rhs.curr_->data_;
    }
    const T& operator*()const{
        return curr_->data_;
    }
};
分类列表{
结构节点{
T数据;
节点*下一个节点;
节点*上一个节点;
节点(常数T&data=T{},节点*nx=nullptr,节点*pr=nullptr){
数据=数据;
下一步=nx;
prev_uu=pr;
}
};
节点*front_u2;;
节点*back_u2;;
公众:
类常量迭代器{
朋友分类表;
受保护的:
节点*电流;
常量迭代器(节点*n){
电流=n;
}
公众:
常量迭代器(){
curr_uu=nullptr;
}
常量迭代器运算符++(){
当前=当前->下一步;
归还*这个;
}
常量迭代器运算符++(int){//advancess curr返回旧的
常量迭代器old=*this;
当前=当前->下一步;
返老还童;
}
常量迭代器运算符--(){
当前=当前->上一个;
归还*这个;
}
常量迭代器运算符--(int){
常量迭代器temp=*this;
当前=当前->上一个;
返回温度;
}
布尔运算符==(常量迭代器rhs){
返回当前->数据==rhs.当前->数据;
}
布尔运算符!=(常量迭代器rhs){
返回当前->数据-=rhs.当前->数据;
}
布尔运算符数据data;
}
常量T&运算符*()常量{
返回当前->数据;
}
};
正在抛出转换错误的位置:

template <typename T>
typename SortedList<T>::iterator SortedList<T>::insert(const T& data){
   Node* n(data);
   iterator it_temp(n);
   iterator sort(front_);
   while (sort < it_temp){
       ++sort;
}
   n.next_ = sort.curr_; 
   n.prev_ = sort.curr_->prev_;
   sort.curr_->prev_->next_ = n;
   sort.curr_->prev_ = n;
}
模板
typename SortedList::iterator SortedList::insert(常量和数据){
节点*n(数据);
迭代器it_temp(n);
迭代器排序(前置);
while(排序上一个;
sort.curr->prev->next\un;
sort.curr->prev\un=n;
}

在insert函数中构造Node*n的地方抛出错误。

不能这样构造指针

Node* n(data);
例如,您必须使用
new

Node* n = new Node(data);

非常感谢,我被这个错误困扰了至少两个小时。