c++模版编程问题,求解救

sysabod sysabod 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
请看代码,感觉不是很难理解,但是出错信息令我头都昏了。

#include <iostream>
#include <deque>
using namespace std;
template <class T>
class Stack
{
private:
deque<T> elems;
public:
Stack();
Stack(int n);
Stack(const Stack<T>&);
void push(const T&);
void pop();
T top()const;
bool isEmpty()const
{
return elems.empty();
}
template <class T2>
Stack<T>& operator= (const Stack<T2>&);
};
template <class T>
Stack<T>::Stack()
{
;
}

template <class T>
Stack<T>::Stack(int n)
{
elems = *(new deque<T>(n,0));
}
template <class T>
Stack<T>:: Stack(const Stack<T>& another)
{
this->elems = *new deque<T>(another);
}
template <class T>
void Stack<T>::push(const T& arg)
{
elems.push_front(arg);
}

template <class T>
void Stack<T>::pop()
{
elems.pop_front();
}
template <class T>
T Stack<T>::top()const
{
return elems.front();
}

template <class T>
template <class T2>
Stack<T>& Stack<T>::operator=(const Stack<T2>& operand2)
{
if((void*)this == (void*)&operand2)
{
return *this;
}
Stack<T2> tmp(operand2);
elems.clear();
while(tmp.isEmpty() == false)
{
this->push(tmp.top());
tmp.pop();
}
return *this;
}

template <class T>
void fillStack(Stack<T>& s, T* begin,T* end )
{
for(T* p = begin; p != end; p++)
{
s.push(*p);
}
}

template <class T>
void print(const Stack<T>& s )
{
while(!s.isEmpty())
{
cout<<s.top()<<" ";
s.pop();
}
cout<<endl;
}
int main(int argc, char *argv[])
{
typedef int IntegerArray[];
typedef float FloatArray[];
IntegerArray a = {1,2,3,40};
FloatArray b = {11.1,22.3,33.24,44.15,50.25};
Stack<int> intStack;
Stack<float> floatStack;
int a_len = sizeof(a)/sizeof(int);
int b_len = sizeof(b)/sizeof(int);
fillStack<int>(intStack,a,a+a_len);
cout<<"Initial int stack:";
print<int>(intStack);



fillStack<float>(floatStack,b,b+b_len);
cout<<"Initial float stack:";
print<float>(floatStack);

intStack = floatStack;
cout<<"after assignment,int stack:", print<int>(intStack);
cout<<"after assignment,float stack:",print<float>(floatStack);

//print();

return 0;
}


下面是我的出错信息

--------------------配置: MinGW3.4.5 - CUI Debug, 编译器类型: MinGW--------------------

正在编译 C:\Documents and Settings\eb\桌面\templateClassAssignment.cpp...
[Error] C:\Documents and Settings\eb\桌面\templateClassAssignment.cpp:105: instantiated from here
[Error] C:\Documents and Settings\eb\桌面\templateClassAssignment.cpp:89: error: passing `const Stack<int>' as `this' argument of `void Stack<T>::pop() [with T = int]' discards qualifiers
[Error] C:\Documents and Settings\eb\桌面\templateClassAssignment.cpp:111: instantiated from here
[Error] C:\Documents and Settings\eb\桌面\templateClassAssignment.cpp:89: error: passing `const Stack<float>' as `this' argument of `void Stack<T>::pop() [with T = float]' discards qualifiers
[Error] C:\Documents and Settings\eb\桌面\templateClassAssignment.cpp:113: instantiated from here
[Warning] C:\Documents and Settings\eb\桌面\templateClassAssignment.cpp:68: warning: passing `float' for converting 1 of `void Stack<T>::push(const T&) [with T = int]'
[Error] C:\Documents and Settings\eb\桌面\templateClassAssignment.cpp:64: instantiated from `Stack<T>& Stack<T>::operator=(const Stack<T2>&) [with T2 = float, T = int]'
[Error] C:\Documents and Settings\eb\桌面\templateClassAssignment.cpp:113: instantiated from here
[Error] C:\Documents and Settings\eb\桌面\templateClassAssignment.cpp:37: error: no matching function for call to `std::deque<float, std::allocator<float> >::deque(const Stack<float>&)'
[Error] C:\PROGRA~1\C-FREE~1\mingw32\include\C__~1\342EBD~1.5\bits\stl_deque.h:670: note: candidates are: std::deque<_Tp, _Alloc>::deque(const std::deque<_Tp, _Alloc>&) [with _Tp = float, _Alloc = std::allocator<float>]
[Error] C:\PROGRA~1\C-FREE~1\mingw32\include\C__~1\342EBD~1.5\bits\stl_deque.h:659: note: std::deque<_Tp, _Alloc>::deque(size_t) [with _Tp = float, _Alloc = std::allocator<float>]
[Error] C:\PROGRA~1\C-FREE~1\mingw32\include\C__~1\342EBD~1.5\bits\stl_deque.h:647: note: std::deque<_Tp, _Alloc>::deque(size_t, const _Tp&, const typename std::_Deque_base<_Tp, _Alloc>::allocator_type&) [with _Tp = float, _Alloc = std::allocator<float>]
[Error] C:\PROGRA~1\C-FREE~1\mingw32\include\C__~1\342EBD~1.5\bits\stl_deque.h:636: note: std::deque<_Tp, _Alloc>::deque(const typename std::_Deque_base<_Tp, _Alloc>::allocator_type&) [with _Tp = float, _Alloc = std::allocator<float>]

编译中止 C:\Documents and Settings\eb\桌面\templateClassAssignment.cpp: 12 个错误, 1 个警告
感觉红色那句错了真是很难理解,其他的不是很明白。请大侠不吝指教。
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
sysabod
C++ 语言 65,214 成员 250,514 内容