c++模版编程问题,求解救
请看代码,感觉不是很难理解,但是出错信息令我头都昏了。
下面是我的出错信息
--------------------配置: 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 个警告
感觉红色那句错了真是很难理解,其他的不是很明白。请大侠不吝指教。
#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 个警告
感觉红色那句错了真是很难理解,其他的不是很明白。请大侠不吝指教。
...全文
相关推荐
C++ .h和.cpp文件总结
"这篇文档是关于C++编程中头文件(.h)和源文件(.cpp)的使用总结,旨在帮助新手理解两者的区别和作用。文档提到了头文件通常用于声明类型、函数和宏常量,而源文件则用于实现函数和定义变量。作者探讨
C/C++基础讲解(八十六)之游戏篇(解救人质游戏)
程序主要是基于Turbo C++ 4.0 Windows 7 Windows 8 64Bit Version进行编译完成了,有兴趣的小伙伴可以自己安装下Turbo C++ 4.0 Windows 7 Windows 8 64Bit Version,网上有现成的教程,当然了,如果有问题,也可以和我联系,这里我们主要讲解些源码实例,不多说了,言归正传。 C语言,做为学习其他语言的基础语言,其重要性就不言而喻了.下面我将和大家一起来学习探讨C语言的相关奥秘。 实例1:解救人质游戏
Dev-Cpp 5.4.0编程软件C++下载C语言免费版
Dev-Cpp是一款经典的C++集成开发环境(IDE),特别适合初学者和专业开发者进行C/C++编程。
C++矩阵求逆
在C++编程中,矩阵求逆是一个常见的数学运算,尤其在处理线性代数问题、计算机图形学、控制系统设计等领域有着广泛的应用。
解救人质游戏
编程语言是实现游戏功能的基础。常见的游戏开发语言有C++、C#、Java和Python等。例如,Unity引擎常使用C#,而Unreal Engine则更倾向于C++。


