error: conversion to non scalar type from int to template

问题描述 投票:0回答:0

对于作业,我们必须实现例如带有模板的堆栈。但是我得到一个我无法真正理解的错误:

error: conversion from ‘shared_ptr<Seq_Node<int>>’ to non-scalar type ‘shared_ptr<Seq_Node<ProjectAlpha::T>>’ requested
   12 |     Seq_NodePtr front = std::make_shared<Seq_Node<T>>(data);

(注意

ProjectAlpha
是我们必须使用的命名空间)

#pragma once
#include "ProjektAlpha/stack.hpp"
#include <memory>

namespace ProjectAlpha {
  template<class T> Stack<T>::Stack() :
  num_elements(0),
  head(nullptr)
  {  
  }
  template<class T> void Stack<T>::insert(T& data) {
    Seq_NodePtr front = std::make_shared<Seq_Node<T>>(data);
    this->num_elements++;
    front->next = this->head->next;
    this->head->next = front;
  };
  
  template<class T> void Stack<T>::remove() {
    num_elements--;
    //TODO

  }
  template<class T> size_t Stack<T>::size() {
    return num_elements;
    //TODO
  }
  template<class T> T& Stack<T>::top() {
    //TODO
  }
}
#pragma once
#include "ProjektAlpha/seq_node.hpp"

namespace ProjectAlpha {

  template<class T> Seq_Node<T>::Seq_Node(T& data) :
    data_(data),
    next(nullptr)
    //prev(nullptr)
  {

  };
  
}
#include "ProjektAlpha/stack.hpp"

int main () {
  ProjectAlpha::Stack<int> *test = new ProjectAlpha::Stack<int>();
  delete test;
}

最后一点是我想作为第一个测试编译的。

c++ pointers templates c++17
© www.soinside.com 2019 - 2024. All rights reserved.