注册 登录
编程论坛 C++教室

适配器stack的构造函数问题

hzz063 发布于 2010-08-11 09:26, 1051 次点击
程序代码:
#include <iostream>
#include <vector>
#include <deque>
#include <stack>
using namespace std;



int main(void)
{

    deque<int> deq(10,5);
   
    stack<int> sta(deq);//为什么提醒不能转换呢


    return 0;
}

编译通不过.....
5 回复
#2
pangding2010-08-11 09:46
应该可以呀,它提示的错误是什么?
#3
hzz0632010-08-11 12:53
提示:

--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
e:\练习文件\【c++】\main.cpp(18) : error C2664: '__thiscall std::stack<int,class std::deque<int,class std::allocator<int> > >::std::stack<int,class std::deque<int,class std::allocator<int> > >(const class std::allocator<int> &)' : cannot convert par
ameter 1 from 'class std::deque<int,class std::allocator<int> >' to 'const class std::allocator<int> &'
        Reason: cannot convert from 'class std::deque<int,class std::allocator<int> >' to 'const class std::allocator<int>'
        No constructor could take the source type, or constructor overload resolution was ambiguous
执行 cl.exe 时出错.

main.obj - 1 error(s), 0 warning(s)



我用vc6,  用别的编译器又可以通过哦.....
#4
pangding2010-08-11 16:16
那看来 vc 的 stack 的构造函数不全呀。

标准只规定了一个构造函数,是这样的:
explicit stack(const Container& = Container());

你可以去你的头文件里看看有没有。
#5
hzz0632010-08-11 17:30
恩,找到这个:
     explicit stack(const allocator_type& _Al = allocator_type())
        : c(_Al) {}

其中:     typedef _C::allocator_type allocator_type;
#6
pangding2010-08-11 23:34
要是只有这一个构造函数的话,就完了。你写的那个确实过不了。

标准里就规定了那么一个构造函数,看来 vc6.0 也没提供……

你要愿意玩玩的话,可以自己加一个构造函数。这个函数的实现是这样的:
explicit stack(const Container& __c= Container())
    :c(__c) {}

那个 Container 就是定义时模版的第二个参数。我想 vc 里用的名字可能可标准的有点不太一样,这很正常。
如果你愿意自己改着玩,想着备份一下以前的头文件。不过这个小改应该没问题~
1