函数模板就是一种通用函数(或函数样板), C++语言的编译器利用它可以自动产生一系列函数。 每一个函数适应一种数据类型。 函数模板的一般形式如下:
template < typename type> ret_type func_name ( parameter list )
{
// body of function
}
编译器按照函数模板产生实际函数的过程称为实例化。 从一个函数模板实例化得到的函数称为实例化函数。 在 C++语言中, 函数模板有两种实例化方式, 分别是显式实例化和隐式实例化。显式实例化时要在函数名后面明确指出模板的实参, 例如<int>或者<char>等。 而隐式实例化无需明确指定模板的实参。对于函数形参的不同数据类型,我们一般都要用显式实例化。
因为上机的时候自己敲的栈模板一直编译出错,下来就我的错误说一下栈模板。
栈(Stack)是一种线性存储结构,它具有如下特点:
- 栈中的数据元素遵守”先进后出"(First In Last Out)的原则,简称FILO结构。
- 限定只能在栈顶进行插入和删除操作。
-
栈的相关概念:
- 栈顶与栈底:允许元素插入与删除的一端称为栈顶,另一端称为栈底。
- 压栈:栈的插入操作,叫做进栈,也称压栈、入栈。
- 弹栈:栈的删除操作,也叫做出栈。
以下是我一开始敲的代码:
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 90;
template <typename T>
class Stack
{
private:
T stack[SIZE];
int top;
public :
Stack() {top=0;};
void push(T n);
T pop();
};
template <class T> void Stack(T)::push(T n)
{
if(top==SIZE)
{
cout<<"栈已满!!"<<endl;
return;
}
stack[top]=n;
top++;
}
template <class T> T Stack(T)::pop()
{
if(top==0)
{
cout<<"栈空!!不能出栈!"<<endl;
return 0;
}
top--;
return stack[top];
}
int main()
{
Stack <int> a;//整数栈
a.push(10);
a.push(20);
a.push(30);
cout<<a.pop()<<' ';
cout<<a.pop()<<endl;
Stack <double> b;//浮点数栈
b.push(11.1);
b.push(22.2);
b.push(33.3);
b.push(44.4);
cout<<b.pop()<<' ';
cout<<b.pop()<<endl;
Stack <char> c;//字符栈
c.push('B');
c.push('X');
c.push('h');
cout<<c.pop()<<endl;
return 0;
}
运行出现如下错误:
E:\My_Project\C++ project\模板\test4.cpp(17) : error C2955: 'Stack' : use of class template requires template argument list
E:\My_Project\C++ project\模板\test4.cpp(16) : see declaration of 'Stack'
E:\My_Project\C++ project\模板\test4.cpp(17) : error C2039: 'push' : is not a member of '`global namespace''
E:\My_Project\C++ project\模板\test4.cpp(17) : error C2146: syntax error : missing ';' before identifier 'push'
E:\My_Project\C++ project\模板\test4.cpp(17) : error C2059: syntax error : ';'
E:\My_Project\C++ project\模板\test4.cpp(17) : error C2065: 'T' : undeclared identifier
E:\My_Project\C++ project\模板\test4.cpp(17) : error C2146: syntax error : missing ')' before identifier 'n'
E:\My_Project\C++ project\模板\test4.cpp(17) : error C2501: 'push' : missing storage-class or type specifiers
E:\My_Project\C++ project\模板\test4.cpp(17) : error C2059: syntax error : ')'
E:\My_Project\C++ project\模板\test4.cpp(27) : error C2904: 'T' : template-name already defined as 'int T'
E:\My_Project\C++ project\模板\test4.cpp(27) : error C2039: 'pop' : is not a member of '`global namespace''
E:\My_Project\C++ project\模板\test4.cpp(28) : error C2143: syntax error : missing ';' before '{'
E:\My_Project\C++ project\模板\test4.cpp(28) : error C2447: missing function header (old-style formal list?)
E:\My_Project\C++ project\模板\test4.cpp(39) : error C2062: type 'int' unexpected
E:\My_Project\C++ project\模板\test4.cpp(40) : error C2065: 'a' : undeclared identifier
E:\My_Project\C++ project\模板\test4.cpp(40) : error C2228: left of '.push' must have class/struct/union type
E:\My_Project\C++ project\模板\test4.cpp(41) : error C2228: left of '.push' must have class/struct/union type
E:\My_Project\C++ project\模板\test4.cpp(43) : error C2228: left of '.pop' must have class/struct/union type
E:\My_Project\C++ project\模板\test4.cpp(44) : error C2228: left of '.pop' must have class/struct/union type
E:\My_Project\C++ project\模板\test4.cpp(45) : error C2062: type 'double' unexpected
E:\My_Project\C++ project\模板\test4.cpp(46) : error C2065: 'b' : undeclared identifier
E:\My_Project\C++ project\模板\test4.cpp(46) : error C2228: left of '.push' must have class/struct/union type
E:\My_Project\C++ project\模板\test4.cpp(47) : error C2228: left of '.push' must have class/struct/union type
E:\My_Project\C++ project\模板\test4.cpp(48) : error C2228: left of '.push' must have class/struct/union type
E:\My_Project\C++ project\模板\test4.cpp(49) : error C2228: left of '.push' must have class/struct/union type
E:\My_Project\C++ project\模板\test4.cpp(50) : error C2228: left of '.pop' must have class/struct/union type
E:\My_Project\C++ project\模板\test4.cpp(51) : error C2228: left of '.pop' must have class/struct/union type
E:\My_Project\C++ project\模板\test4.cpp(52) : error C2062: type 'char' unexpected
E:\My_Project\C++ project\模板\test4.cpp(53) : error C2065: 'c' : undeclared identifier
E:\My_Project\C++ project\模板\test4.cpp(53) : error C2228: left of '.push' must have class/struct/union type
E:\My_Project\C++ project\模板\test4.cpp(54) : error C2228: left of '.push' must have class/struct/union type
E:\My_Project\C++ project\模板\test4.cpp(55) : error C2228: left of '.push' must have class/struct/union type
E:\My_Project\C++ project\模板\test4.cpp(56) : error C2228: left of '.pop' must have class/struct/union type
执行 cl.exe 时出错.
test4.obj - 1 error(s), 0 warning(s)
下面是改后的代码:
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 90;
template <typename T>
class Stack
{
private:
T stack[SIZE];
int top;
public :
Stack() {top=0;};
void push(T n);
T pop();
};
template <class T> void Stack<T>::push(T n)
{
if(top==SIZE)
{
cout<<"栈已满!!"<<endl;
return;
}
stack[top]=n;
top++;
}
template <class T> T Stack<T>::pop()
{
if(top==0)
{
cout<<"栈空不能出栈!"<<endl;
return 0;
}
top--;
return stack[top];
}
int main()
{
Stack <int> a;//整数栈
a.push(10);
a.push(20);
//a.push(30);
cout<<a.pop()<<' ';
cout<<a.pop()<<endl;
Stack <double> b;//浮点数栈
b.push(11.1);
b.push(22.2);
b.push(33.3);
b.push(44.4);
cout<<b.pop()<<' ';
cout<<b.pop()<<endl;
Stack <char> c;//字符栈
c.push('B');
c.push('X');
c.push('h');
cout<<c.pop()<<endl;
return 0;
}
改正后的代码完全可以输出正确结果。
emmmm,两个代码的区别是什么呢,,,
一个很尴尬的错误:void Stack和T Stack函数后面的符号不一样,这个小小的错误被我找了半天,以后写程序还是要细心呐!!
出错代码:
template <class T> void Stack(T)::push(T n)
template <class T> T Stack(T)::pop()
正确代码:
template <class T> void Stack<T>::push(T n)
template <class T> T Stack<T>::pop()