C++ 友元函数模板 和 友元类模板 和 顺序栈
来源:互联网 发布:香港阿里云服务器 编辑:程序博客网 时间:2024/04/29 22:55
在开始的开始,强烈鄙视清华大学出版社的殷人昆的数据结构,身为一本教材竟然如此多错误!
今天按照那本该死的数据结构写了一个顺序栈,在友元函数模板处遇到无法识别的标识符错误。
friend ostream & operator << (ostream & os,SeqStack<T> & s);
而在类中的友元模板,应该这样用:
1.友元函数模板
template<class T>class A{public:template <class T> friend void fun();}
2.友元类
template<class T>class A{};template<class T>class B{public: friend class A<T>;}
Stack.h
#pragma onceconst int maxSize = 50;template<class T>class Stack{public:Stack(){};~Stack(){};virtual void Push(const T & x) = 0;virtual bool Pop(T & x) = 0;virtual bool getTop(T & x)const = 0;virtual bool IsEmpty()const = 0;virtual bool IsFull()const = 0;virtual int getSize()const = 0;};
SeqStack.h
#pragma once#include "Stack.h"#include<assert.h>#include<iostream>using std::ostream;using std::cout;using std::endl;using std::cerr;const int stackIncreament = 20;template<class T>class SeqStack :public Stack<T>{public:SeqStack(int sz=50);~SeqStack();void Push(const T & x);bool Pop(T & x);bool getTop(T & x) const;bool IsEmpty() const;bool IsFull() const;int getSize() const;void MakeEmpty();template <class T> friend ostream & operator << (ostream & cout ,SeqStack<T> & s);private:T * elements;int top;int maxSize;void overflowProcess();};template<class T>SeqStack<T>::SeqStack(int sz = 50) :top(-1), maxSize(sz){elements = new T[maxSize];assert(elements != NULL);}template<class T>SeqStack<T>::~SeqStack(){delete[]elements;}template<class T>void SeqStack<T>::Push(const T & x){if (IsFull() == true)overflowProcess();elements[++top] = x;}template<class T>bool SeqStack<T>::Pop(T & x){if (IsEmpty() == true)return false;x = elements[top--];return true;}template<class T>bool SeqStack<T>::getTop(T & x) const{if (IsEmpty() == true)return false;x = elements[top];return true;}template<class T>bool SeqStack<T>::IsEmpty() const{if (top == -1)return true;elsereturn false;}template<class T>bool SeqStack<T>::IsFull() const{if (top + 1 == maxSize)return true;elsereturn false;}template<class T>int SeqStack<T>::getSize() const{return maxSize;}template<class T>void SeqStack<T>::MakeEmpty(){}template<class T>void SeqStack<T>::overflowProcess(){T * newArray = new T[maxSize + stackIncreament];if (newArray = NULL){cerr << "内存分配失败" << endl;exit(1);}for (int i = 0; i <= top; ++i){newArray[i] = elements[i];}maxSize = maxSize + stackIncreament;delete[]elements;elements = newArray;}template<class T>ostream & operator << (ostream & os, SeqStack<T> & s){int i = s.top;for (; i >= 0; --i){os << "#" << i + 1 << " " << s.elements[i] << endl;}return os;}
- C++ 友元函数模板 和 友元类模板 和 顺序栈
- 友元函数和友元类(非模板)
- 模板类和友元函数
- 模板类和友元函数
- 模板类和友元函数
- 模板类和友元函数
- 【c/c++】函数模板和模板函数
- 模板特例和模板元
- 友元模板和编译器
- 模板类和友元
- 模板类和友元
- C++,template,类模板和函数模板
- C++类模板及友元类模板,友元函数模板
- 模板类和友元函数再探
- 模板友元函数
- c++---函数和类模板
- 模板类中包含模板类友元和其他友元函数
- 函数模板和模板函数
- C++第11周项目5——歌手大奖赛计分
- 去掉android的屏幕上的titlebar
- Unusual Memory Management Situations
- 正则表达式
- memset的实现及改进
- C++ 友元函数模板 和 友元类模板 和 顺序栈
- 小波学习之三(多孔算法与MATLAB swt剖析)
- ESRI符号MakeServerStyleSet解决方法
- LDD3 sbull 模块
- 根据有序数组构造二叉查找树 Convert Sorted Array to Binary Search Tree
- android登陆注册
- mod函数用法
- 从Oracle到MySQL,余额宝云实践分享
- 2013-C++第11周项目参考解答链接集