#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> elem;
public:
void push(T const&);
void pop();
T top() const;
bool empty() const {return elems.empty();}
template <typename T2
template<typename ELEM2,typename=std::allocator<ELEM2> > class CONT2>
Stack<T,CONT>& operator=(Stack<T2,CONT2> const&);
};
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(elem.empty())
{
throw std::out_of_range("Stack<>::pop():empty stack");
}
elem.pop_back();
}
template<typename T,template<typename,typename> class CONT>
T Stack<T,CONT> top()const
{
if(elem.empty())
{
throw std::out_of_range("stack<>::top():empty stack");
}
return elem.back();
}
template<typename T,template<typename,typename>class CONT>
template<typename T2,template<typename,typename> class CONT2>
Stack<T,CONT> &Stack<T,CONT>::operator=(Stack<T2,CONT2> const&op2)
{
if((void*)this==(void*)&op2)
return*this;
Stack<T2,CONT2>tem(op2);
elem.clear();
while(!tem.empty())
{
elem.push_front(tem.top());
tmp.pop();
}
return *this;
}
main文件
#include <iostream>
#include <string>
#include <cstdlib>
#include "a.h"
int main()
{
try
{
Stack<int> intStack;
Stack<float> floatStack;
intStack.push(42);
intStack.push(7);
floatStack.push(7.7);
floatStack=intStack;
std::cout<<floatStack.top()<<std::endl;
floatStack.pop();
std::cout<<floatStack.top()<<std::endl;
floatStack.pop();
std::cout<<floatStack.top()<<std::endl;
floatSatck.pop();
}
catch(std::exception const& e)
{
std::cerr<<"exception:"<<e.what()<<std::endl;
}
}
结果出现错误如下:
Compiling...
cc.cpp
f:\vc\msdev98\myprojects\dd\a.h(6) : error C2954: template definitions cannot nest
f:\vc\msdev98\myprojects\dd\a.h(6) : error C2951: template declarations are only permitted at global or namespace scope
f:\vc\msdev98\myprojects\dd\a.h(6) : error C2226: syntax error : unexpected type 'CONT'
f:\vc\msdev98\myprojects\dd\a.h(8) : error C2143: syntax error : missing '>' before '{'
f:\vc\msdev98\myprojects\dd\a.h(19) : error C2059: syntax error : '<end Parse>'
f:\vc\msdev98\myprojects\dd\a.h(20) : error C2954: template definitions cannot nest
f:\vc\msdev98\myprojects\dd\a.h(20) : error C2954: template definitions cannot nest
f:\vc\msdev98\myprojects\dd\a.h(20) : error C2951: template declarations are only permitted at global or namespace scope
f:\vc\msdev98\myprojects\dd\a.h(20) : error C2991: redefinition of template parameter '__unnamed'
f:\vc\msdev98\myprojects\dd\a.h(20) : error C2226: syntax error : unexpected type 'CONT'
f:\vc\msdev98\myprojects\dd\a.h(22) : error C2143: syntax error : missing '>' before '{'
f:\vc\msdev98\myprojects\dd\a.h(24) : error C2059: syntax error : '<end Parse>'
f:\vc\msdev98\myprojects\dd\a.h(26) : error C2039: 'pop' : is not a member of '`global namespace''
f:\vc\msdev98\myprojects\dd\a.h(27) : error C2143: syntax error : missing ';' before '{'
f:\vc\msdev98\myprojects\dd\a.h(27) : error C2447: missing function header (old-style formal list?)
f:\vc\msdev98\myprojects\dd\a.h(34) : error C2954: template definitions cannot nest
f:\vc\msdev98\myprojects\dd\a.h(34) : error C2954: template definitions cannot nest
f:\vc\msdev98\myprojects\dd\a.h(34) : error C2951: template declarations are only permitted at global or namespace scope
f:\vc\msdev98\myprojects\dd\a.h(34) : error C2991: redefinition of template parameter '__unnamed'
f:\vc\msdev98\myprojects\dd\a.h(34) : error C2226: syntax error : unexpected type 'CONT'
f:\vc\msdev98\myprojects\dd\a.h(36) : error C2143: syntax error : missing '>' before '{'
f:\vc\msdev98\myprojects\dd\a.h(42) : error C2059: syntax error : '<end Parse>'
f:\vc\msdev98\myprojects\dd\a.h(45) : error C2039: '=' : is not a member of '`global namespace''
f:\vc\msdev98\myprojects\dd\a.h(46) : error C2143: syntax error : missing ';' before '{'
f:\vc\msdev98\myprojects\dd\a.h(46) : error C2447: missing function header (old-style formal list?)
F:\vc\MSDev98\MyProjects\dd\cc.cpp(9) : error C2065: 'Stack' : undeclared identifier
F:\vc\MSDev98\MyProjects\dd\cc.cpp(9) : error C2062: type 'int' unexpected
F:\vc\MSDev98\MyProjects\dd\cc.cpp(10) : error C2062: type 'float' unexpected
F:\vc\MSDev98\MyProjects\dd\cc.cpp(11) : error C2065: 'intStack' : undeclared identifier
F:\vc\MSDev98\MyProjects\dd\cc.cpp(11) : error C2228: left of '.push' must have class/struct/union type
F:\vc\MSDev98\MyProjects\dd\cc.cpp(12) : error C2228: left of '.push' must have class/struct/union type
F:\vc\MSDev98\MyProjects\dd\cc.cpp(13) : error C2065: 'floatStack' : undeclared identifier
F:\vc\MSDev98\MyProjects\dd\cc.cpp(13) : error C2228: left of '.push' must have class/struct/union type
F:\vc\MSDev98\MyProjects\dd\cc.cpp(15) : error C2228: left of '.top' must have class/struct/union type
F:\vc\MSDev98\MyProjects\dd\cc.cpp(16) : error C2228: left of '.pop' must have class/struct/union type
F:\vc\MSDev98\MyProjects\dd\cc.cpp(17) : error C2228: left of '.top' must have class/struct/union type
F:\vc\MSDev98\MyProjects\dd\cc.cpp(18) : error C2228: left of '.pop' must have class/struct/union type
F:\vc\MSDev98\MyProjects\dd\cc.cpp(19) : error C2228: left of '.top' must have class/struct/union type
F:\vc\MSDev98\MyProjects\dd\cc.cpp(20) : error C2065: 'floatSatck' : undeclared identifier
F:\vc\MSDev98\MyProjects\dd\cc.cpp(20) : error C2228: left of '.pop' must have class/struct/union type
F:\vc\MSDev98\MyProjects\dd\cc.cpp(24) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
cc.exe - 41 error(s), 0 warning(s)
不知道为什么 ~(VC6)
请指教~呵呵,太菜了
8 个解决方案
#1
你先检查一下你的代码是不是少了","或者之类的东西,另外vc6.0偏特化都不支持,不知道的的代码支持不
#2
主要是拼写错误(CONT<T> elem;改为CONT<T> elems;)
,改正后可以在dev cpp通过:
#include <deque>
#include <stdexcept>
#include <memory>
#include <iostream>
#include <string>
#include <cstdlib>
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 T2,
template<typename ELEM2,typename=std::allocator<ELEM2> > class CONT2>
Stack<T,CONT>& operator=(Stack<T2,CONT2> const&);
};
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();
}
template<typename T,template<typename,typename>class CONT>
template<typename T2,template<typename,typename> class CONT2>
Stack<T,CONT> &Stack<T,CONT>::operator=(Stack<T2,CONT2> const&op2)
{
if((void*)this==(void*)&op2)
return*this;
Stack<T2,CONT2>tem(op2);
elems.clear();
while(!tem.empty())
{
elems.push_front(tem.top());
tem.pop();
}
return *this;
}
int main()
{
try
{
Stack<int> intStack;
Stack<float> floatStack;
intStack.push(42);
intStack.push(7);
floatStack.push(7.7);
floatStack=intStack;
std::cout<<floatStack.top()<<std::endl;
floatStack.pop();
std::cout<<floatStack.top()<<std::endl;
floatStack.pop();
std::cout<<floatStack.top()<<std::endl;
floatStack.pop();
}
catch(std::exception const& e)
{
std::cerr<<"exception:"<<e.what()<<std::endl;
}
}
,改正后可以在dev cpp通过:
#include <deque>
#include <stdexcept>
#include <memory>
#include <iostream>
#include <string>
#include <cstdlib>
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 T2,
template<typename ELEM2,typename=std::allocator<ELEM2> > class CONT2>
Stack<T,CONT>& operator=(Stack<T2,CONT2> const&);
};
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();
}
template<typename T,template<typename,typename>class CONT>
template<typename T2,template<typename,typename> class CONT2>
Stack<T,CONT> &Stack<T,CONT>::operator=(Stack<T2,CONT2> const&op2)
{
if((void*)this==(void*)&op2)
return*this;
Stack<T2,CONT2>tem(op2);
elems.clear();
while(!tem.empty())
{
elems.push_front(tem.top());
tem.pop();
}
return *this;
}
int main()
{
try
{
Stack<int> intStack;
Stack<float> floatStack;
intStack.push(42);
intStack.push(7);
floatStack.push(7.7);
floatStack=intStack;
std::cout<<floatStack.top()<<std::endl;
floatStack.pop();
std::cout<<floatStack.top()<<std::endl;
floatStack.pop();
std::cout<<floatStack.top()<<std::endl;
floatStack.pop();
}
catch(std::exception const& e)
{
std::cerr<<"exception:"<<e.what()<<std::endl;
}
}
#3
vc6下不支持模板的嵌套
你的第一个信息就是这个吧
你的第一个信息就是这个吧
#4
估计是VC6的毛病 ~
#5
我为了你感到骄傲!
#6
学习
#7
可能是版本问题,你用2003编译试试.
#8
版本问题,VC6对template支持的很不好~
回复人: MagicCarmack(梦中情人C++) ( ) 信誉:100 2005-4-9 3:20:44 得分: 0
我为了你感到骄傲!
---------------------------------
什么意思?》
回复人: MagicCarmack(梦中情人C++) ( ) 信誉:100 2005-4-9 3:20:44 得分: 0
我为了你感到骄傲!
---------------------------------
什么意思?》
#1
你先检查一下你的代码是不是少了","或者之类的东西,另外vc6.0偏特化都不支持,不知道的的代码支持不
#2
主要是拼写错误(CONT<T> elem;改为CONT<T> elems;)
,改正后可以在dev cpp通过:
#include <deque>
#include <stdexcept>
#include <memory>
#include <iostream>
#include <string>
#include <cstdlib>
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 T2,
template<typename ELEM2,typename=std::allocator<ELEM2> > class CONT2>
Stack<T,CONT>& operator=(Stack<T2,CONT2> const&);
};
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();
}
template<typename T,template<typename,typename>class CONT>
template<typename T2,template<typename,typename> class CONT2>
Stack<T,CONT> &Stack<T,CONT>::operator=(Stack<T2,CONT2> const&op2)
{
if((void*)this==(void*)&op2)
return*this;
Stack<T2,CONT2>tem(op2);
elems.clear();
while(!tem.empty())
{
elems.push_front(tem.top());
tem.pop();
}
return *this;
}
int main()
{
try
{
Stack<int> intStack;
Stack<float> floatStack;
intStack.push(42);
intStack.push(7);
floatStack.push(7.7);
floatStack=intStack;
std::cout<<floatStack.top()<<std::endl;
floatStack.pop();
std::cout<<floatStack.top()<<std::endl;
floatStack.pop();
std::cout<<floatStack.top()<<std::endl;
floatStack.pop();
}
catch(std::exception const& e)
{
std::cerr<<"exception:"<<e.what()<<std::endl;
}
}
,改正后可以在dev cpp通过:
#include <deque>
#include <stdexcept>
#include <memory>
#include <iostream>
#include <string>
#include <cstdlib>
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 T2,
template<typename ELEM2,typename=std::allocator<ELEM2> > class CONT2>
Stack<T,CONT>& operator=(Stack<T2,CONT2> const&);
};
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();
}
template<typename T,template<typename,typename>class CONT>
template<typename T2,template<typename,typename> class CONT2>
Stack<T,CONT> &Stack<T,CONT>::operator=(Stack<T2,CONT2> const&op2)
{
if((void*)this==(void*)&op2)
return*this;
Stack<T2,CONT2>tem(op2);
elems.clear();
while(!tem.empty())
{
elems.push_front(tem.top());
tem.pop();
}
return *this;
}
int main()
{
try
{
Stack<int> intStack;
Stack<float> floatStack;
intStack.push(42);
intStack.push(7);
floatStack.push(7.7);
floatStack=intStack;
std::cout<<floatStack.top()<<std::endl;
floatStack.pop();
std::cout<<floatStack.top()<<std::endl;
floatStack.pop();
std::cout<<floatStack.top()<<std::endl;
floatStack.pop();
}
catch(std::exception const& e)
{
std::cerr<<"exception:"<<e.what()<<std::endl;
}
}
#3
vc6下不支持模板的嵌套
你的第一个信息就是这个吧
你的第一个信息就是这个吧
#4
估计是VC6的毛病 ~
#5
我为了你感到骄傲!
#6
学习
#7
可能是版本问题,你用2003编译试试.
#8
版本问题,VC6对template支持的很不好~
回复人: MagicCarmack(梦中情人C++) ( ) 信誉:100 2005-4-9 3:20:44 得分: 0
我为了你感到骄傲!
---------------------------------
什么意思?》
回复人: MagicCarmack(梦中情人C++) ( ) 信誉:100 2005-4-9 3:20:44 得分: 0
我为了你感到骄傲!
---------------------------------
什么意思?》