首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

精通Template技术的!小弟我写的摸板的摸板出异常

2012-04-05 
精通Template技术的高手请进!我写的摸板的摸板出错误!//T55#ifndefSTACK_H_55#defineSTACK_H_55#include

精通Template技术的高手请进!我写的摸板的摸板出错误!
//T55
#ifndef     STACK_H_55
#define     STACK_H_55

#include <deque>
#include <stdexcept>
#include <memory>

template <typename   T,
                              template <typename     ELEM,
  typename=std::allocator <ELEM>   > class   CONT=std::deque>
class   Stack
{
private:
CONT <T>       elems;     //元素;
public:
void     push(T   const   &);
void     pop();
        T       top()const;
bool     empty()   const
{
return     elems.empty();
}
};

template <typename   T,
                              template <typename    
                                typename   > class   CONT>
void   Stack <T,CONT> ::push(T   const   &   elem)
{
elems.push_back(elem);
}
template <typename   T,
                              template <typename
                                        typename>   class   CONT>
void   Stack <T,CONT> ::pop()
{
if(elems.empty())
{
throw   std::out_of_range( "Stack <> ::pop():   empty   stack! ");
}
elems.pop_back();
}

template <typename   T,
                              template <typename
                                        typename>   class   CONT>
T   Stack <T,CONT> ::top()const
{
if(elems.empty())
{
throw   std::out_of_range( "Stack <> ::top:   empty   stack! ");
}
return   elems.back();
}
#endif;

#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include "T55.h "

int   main()
{
try{
Stack <int>             intStack;
                intStack.push(42);        
  intStack.push(7);       //   intStack.push(15);
  std::cout < <intStack.top() < <std::endl;
}catch(std::exception     const   &   ex){
std::cerr < < "EXception:   " < <ex.what() < <std::endl;
}
}
VC++2005:
出错提示如下:

T55.cpp
e:\c++分支\template\0530\0530\t55.h(31)   :   error   C3855:   'Stack <T,CONT> ':   template   parameter   'CONT '   is   incompatible   with   the   declaration
e:\c++分支\template\0530\0530\t55.h(42)   :   error   C3855:   'Stack <T,CONT> ':   template   parameter   'CONT '   is   incompatible   with   the   declaration


e:\c++分支\template\0530\0530\t55.h(53)   :   error   C3855:   'Stack <T,CONT> ':   template   parameter   'CONT '   is   incompatible   with   the   declaration
e:\c++分支\template\0530\0530\t55.cpp(11)   :   error   C2264:   'Stack <T> ::push '   :   error   in   function   definition   or   declaration;   function   not   called
                with
                [
                        T=int
                ]
e:\c++分支\template\0530\0530\t55.cpp(12)   :   error   C2264:   'Stack <T> ::push '   :   error   in   function   definition   or   declaration;   function   not   called
                with
                [
                        T=int
                ]
e:\c++分支\template\0530\0530\t55.cpp(13)   :   error   C2264:   'Stack <T> ::top '   :   error   in   function   definition   or   declaration;   function   not   called
                with
                [
                        T=int
                ]


[解决办法]
建议,模板参数用
template <typename T,
class CONT=std::deque <T> >
class Stack{
CONT elems;
....
这样解决了问题,也增加了灵活性
[解决办法]
template <typename T,
template <typename
typename > class CONT>
你两个typename间没加 ", "加了就通过了。
template <typename T,template <typename,typename > class CONT>

热点排行