zhxue_11 2019-01-04 23:06 采纳率: 0%
浏览 706

leetcode100,想使用智能指针但是出错,请问为什么?

代码如下:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        stack<shared_ptr<TreeNode>> s1, s2;
        if (p) {
            s1.push(shared_ptr<TreeNode>(p));
        }
        if (q) {
            s1.push(make_shared<TreeNode>(q));
        }
        while (!s1.empty() && !s2.empty()) {
            shared_ptr<TreeNode> pT = s1.top(); s1.pop();
            shared_ptr<TreeNode> qT = s2.top(); s2.pop();
            if (pT->val != qT->val) {
                return false;
            }
            if (pT->left) {
                s1.push(make_shared<TreeNode>(pT->left));
            }
            if (qT->left) {
                s2.push(make_shared<TreeNode>(qT->left));
            }
            if (s1.size() != s2.size()) {
                return false;
            }
            if (pT->right) {
                s1.push(make_shared<TreeNode>(pT->right));
            }
            if (qT->right) {
                s2.push(make_shared<TreeNode>(qT->right));
            }
            if (s1.size() != s2.size()) {
                return false;
            }
        }
        return false;
    }
};

代码错误提示如下:
In file included from /usr/local/include/c++/6.3.0/x86_64-linux-gnu/bits/c++allocator.h:33:0,
from /usr/local/include/c++/6.3.0/bits/allocator.h:46,
from /usr/local/include/c++/6.3.0/string:41,
from /usr/local/include/c++/6.3.0/bits/locale_classes.h:40,
from /usr/local/include/c++/6.3.0/bits/ios_base.h:41,
from /usr/local/include/c++/6.3.0/ios:42,
from /usr/local/include/c++/6.3.0/ostream:38,
from /usr/local/include/c++/6.3.0/iostream:39,
from headers.h:1:
/usr/local/include/c++/6.3.0/ext/new_allocator.h: In instantiation of 'void gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = TreeNode; _Args = {TreeNode*&}; _Tp = TreeNode]':
/usr/local/include/c++/6.3.0/bits/alloc_traits.h:455:4: required from 'static void std::allocator_traitsstd::allocator<_CharT >::construct(std::allocator_traitsstd::allocator<_CharT >::allocator_type&, _Up*, _Args&& ...) [with _Up = TreeNode; _Args = {TreeNode*&}; _Tp = TreeNode; std::allocator_traitsstd::allocator<_CharT >::allocator_type = std::allocator]'
/usr/local/include/c++/6.3.0/bits/shared_ptr_base.h:520:39: required from 'std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {TreeNode*&}; _Tp = TreeNode; _Alloc = std::allocator; __gnu_cxx::_Lock_policy _Lp = (
gnu_cxx::_Lock_policy)2u]'
/usr/local/include/c++/6.3.0/bits/shared_ptr_base.h:615:4: required from 'std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, Tp*, const _Alloc&, _Args&& ...) [with _Tp = TreeNode; _Alloc = std::allocator; _Args = {TreeNode*&}; __gnu_cxx::_Lock_policy _Lp = (_gnu_cxx::_Lock_policy)2u]'
/usr/local/include/c++/6.3.0/bits/shared_ptr_base.h:1100:35: required from 'std::__shared_ptr<_Tp, Lp>::_shared_ptr(std::_Sp_make_shared_tag, const Alloc&, _Args&& ...) [with _Alloc = std::allocator; _Args = {TreeNode*&}; _Tp = TreeNode; __gnu_cxx::_Lock_policy _Lp = (_gnu_cxx::_Lock_policy)2u]'
/usr/local/include/c++/6.3.0/bits/shared_ptr.h:319:64: required from 'std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const Alloc&, _Args&& ...) [with _Alloc = std::allocator; _Args = {TreeNode*&}; _Tp = TreeNode]'
/usr/local/include/c++/6.3.0/bits/shared_ptr.h:619:14: required from 'std::shared_ptr<_Tp1> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = TreeNode; _Alloc = std::allocator; _Args = {TreeNode*&}]'
/usr/local/include/c++/6.3.0/bits/shared_ptr.h:635:39: required from 'std::shared_ptr<_Tp1> std::make_shared(_Args&& ...) [with _Tp = TreeNode; _Args = {TreeNode*&}]'
Line 18: Char 44: required from here
/usr/local/include/c++/6.3.0/ext/new_allocator.h:120:4: error: invalid conversion from 'TreeNode*' to 'int' [-fpermissive]
{ ::new((void *)
_p) Up(std::forward<_Args>(_args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

我寻思使用make_shared<>也不会让TreeNode*变成int啊,而且直接使用构造函数就可以,太奇怪了
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)