无法从std :: shared_ptr <_Ty>转换为std :: shared_ptr <_Ty>

时间:2018-11-25 10:21:19

标签: c++ pointers casting shared-ptr smart-pointers

我遇到以下错误:

  

错误C2440:'static_cast':无法从'std :: shared_ptr <_Ty>'转换为'std :: shared_ptr <_Ty> stack \ genericstack.h 36 1 Stack

GenericStack.h

#ifndef _GENERIC_STACK_TROFIMOV_H_
#define _GENERIC_STACK_TROFIMOV_H_

#include <memory>

class GenericStack {
    struct StackNode {
        std::shared_ptr<void> _data; 
        StackNode* _next;
        StackNode(const std::shared_ptr<void>& data, StackNode* next) 
            : _data(data), _next(next) {

        }
    };

    StackNode* _top; 

    GenericStack(const GenericStack&);
    GenericStack& operator=(const GenericStack&);

protected:
    GenericStack();
    ~GenericStack();
    void push(const std::shared_ptr<void>&);
    void pop();
    std::shared_ptr<void>& top();
    bool isEmpty() const;
};

template <class T>
class TStack: private GenericStack {                  
public:
    void push(const std::shared_ptr<T>& p) { GenericStack::push(p); }
    void pop() { GenericStack::pop(); }
    std::shared_ptr<T> top() { return static_cast<std::shared_ptr<T>>(GenericStack::top()); }
    bool empty() const { return GenericStack::isEmpty(); }
};

#endif

GenerickStack.cpp

#include "GenericStack.h"

GenericStack::GenericStack()
    :_top(0) {

};
GenericStack::~GenericStack() {
    while(!isEmpty()) {
        pop();
    }
};

void GenericStack::push(const std::shared_ptr<void>& element) {
    _top = new StackNode(element, _top);
}

std::shared_ptr<void>& GenericStack::top() {
    return _top->_data;
}
void GenericStack::pop() {
    StackNode* t = _top->_next;
    delete _top;
    _top = t;
}

bool GenericStack::isEmpty() const {
    return !_top;
}

Main.cpp

#include <iostream>
#include "GenericStack.h"

int main() {
    TStack<int> gs;

    std::shared_ptr<int> sh(new int(7));
    gs.push(sh);
    std::cout << *gs.top() << std::endl;

    return 0;
}

我为什么会收到错误消息?

我希望强制转换能够成功进行,因为使用原始指针,我总是可以将void*转换为实型指针。

我在这里要做的是创建一个堆栈模板。但是我正在尝试重用尽可能多的代码,以使模板化的类不会膨胀太多。

谢谢。

2 个答案:

答案 0 :(得分:1)

看看the list of constructors for shared_ptr。您正在尝试使用重载9,更具体地说是使用Y = voidT = int的模板重载。但是,此模板重载不参与重载解析,因为void*不能隐式转换为int*。换句话说,如果无法将shared_ptr<void>隐式转换为shared_ptr<T>,则无法将void*转换为T*

为什么不首先使用模板(将GenericStack功能移至TStack),而不是尝试处理void*

  

但是我正在尝试尽可能多地重用代码,以使模板化的类不会膨胀太多。

通过“膨胀”,我假设您的意思是模板解决方案将生成太多实例?您是否有任何理由相信确实会太多?

答案 1 :(得分:0)

您会收到此错误,因为from要求类型toshared_ptr是可转换的。对于void*仅在c'tor overload 9参与重载解析时才成立。但这不是,因为static_cast不能隐式转换为C ++中的其他对象指针类型,因此它需要显式的static_casting

如果要基于 std::shared_ptr<T> top() { return std::static_pointer_cast<T>(GenericStack::top()); } 托管指针类型转换共享指针,则需要使用std::static_pointer_cast,这就是它的用途。

因此,在插入该修复程序之后

class ContentView: UIView {


    let request = requestQuestion()
    var currentVC:ViewController?
    var times = "10"
    let time = UILabel()
    let textViews = UILabel()
    let recommendList = recommend()
    let choices = ChoicesCollections()
    var content = ""

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupView(){
        content = request.getquestion(questions: "212", url: "http://localhost/memory/question.php")
        print(content)
        time.translatesAutoresizingMaskIntoConstraints = false
        textViews.translatesAutoresizingMaskIntoConstraints = false
        recommendList.translatesAutoresizingMaskIntoConstraints = false
        choices.translatesAutoresizingMaskIntoConstraints = false

        addSubview(time)
        addSubview(textViews)
        addSubview(recommendList)
        addSubview(choices)

        time.text = times
        time.font = UIFont.boldSystemFont(ofSize: 20)
        time.textAlignment = .center
        time.topAnchor.constraint(equalTo: topAnchor, constant: 40).isActive = true
        time.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0).isActive = true
        time.widthAnchor.constraint(equalToConstant: 50).isActive = true
        time.heightAnchor.constraint(equalToConstant: 25).isActive = true

        textViews.numberOfLines = 7
        textViews.font = UIFont.boldSystemFont(ofSize: 30)
        textViews.text = content
        textViews.textAlignment = .center
        textViews.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
        textViews.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        textViews.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
        textViews.bottomAnchor.constraint(equalTo: centerYAnchor, constant: 100).isActive = true

        choices.topAnchor.constraint(equalTo: textViews.bottomAnchor, constant: 60).isActive = true
        choices.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        choices.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
        choices.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true

        recommendList.backgroundColor = .black
        recommendList.bottomAnchor.constraint(equalTo: choices.topAnchor,constant: -5).isActive = true
        recommendList.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        recommendList.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
        recommendList.heightAnchor.constraint(equalToConstant: 50).isActive = true
    }
}

您的薄模板包装器会很好地构建。