| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦   
共有 203 人关注过本帖
标题:超简单自定义类模板栈(友元失效???请进)
收藏  订阅  推荐  打印
晨曦的朝阳
Rank: 2
等级:注册会员
帖子:66
积分:866
注册:2008-1-24
超简单自定义类模板栈(友元失效???请进)

有个头文件如下(简单的自定义栈类)   //问题后面有说明
//Stack.h
#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;
const int CAPACITY=100;
template <class ElementType>
ostream& operator < <(ostream & out,const Stack <ElementType> & st);  //提示说这里有错,汗

template <class ElementType>
class Stack
{
      public:
            Stack();//枸造函数
              ~Stack(){}//析构函数
              bool empty()const;//判断栈是否为空
              void push(const ElementType &);//压入栈顶
              ElementType top()const;//返回栈顶元素
              void pop();//删除栈顶元素
    friend ostream& operator < <(ostream & out,const Stack <ElementType> & st);
                                    
      private:
              int MyTop;
              ElementType Stack_Array[CAPACITY];
};

template <class ElementType>
inline Stack <ElementType>::Stack()
{
      MyTop=-1;
}

template <class ElementType>
bool Stack <ElementType>::empty()const
{
    if(MyTop==-1)
        return true;
    else
        return false;
}

template <class ElementType>
void Stack <ElementType>::push(const ElementType &value)
{
    if(MyTop < CAPACITY-1)
    {
          MyTop++;
          Stack_Array[MyTop]=value;
    }
    else
          cout < <"Stack_full,please change the CAPACITY in the Stack.h\n";
}  

template <class ElementType>
ElementType Stack <ElementType>::top()const
{
    if(MyTop!=-1)
        return Stack_Array[MyTop];
    else
        cout < <"Stack_empty!\n";
}  

template <class ElementType>
void Stack <ElementType>::pop()
{
    if(MyTop!=-1)
        MyTop--;
    else
        cout < <"Stack_empty! Cann't delete!\n";
}

template <class ElementType>
ostream& operator < <(ostream & out,const Stack <ElementType> & st)
{

for(int pos=st.MyTop; pos>=0; pos--)
out < <st.Stack_Array[pos] < <endl;
return out;
}

#endif


测试文件如下:
#include <iostream>
#include "Stack.h"
using namespace std;
int main()
{
    Stack <int> intSt;
    for(int i=1; i <=4; i++)
        intSt.push(i);
    while(!intSt.empty())
{
cout < <intSt.top() < <endl;
intSt.pop();
}
    for(i=1; i <=4; i++)
        intSt.push(i);
cout < <intSt;
    getchar();
    return 0;
}
错误提示:                     
...\Stack.h(8):error C2143:syntax error:missing ','before ' <'
...\Stack.h(8):error C2059:syntax error:' <'
格式我觉得是没有错的,改了很多次,到底是哪里出问题了?各位帮帮忙,在此谢过!我用的vc++6.0
//template <class ElementType>
//ostream& operator < <(ostream & out,const Stack <ElementType> & st);
如果去掉上面那两行也是不行的,不信试试,说是无法访问私有成员,好像友元失效了???
2008-7-23 21:27
zjl138
Rank: 4
等级:高级会员
帖子:764
积分:9144
威望:1
注册:2007-11-12

你的代码是不是从网上弄来的."<<"都变成"< <"这样了.

VC6的话,就要自已定义名空间了.

i like linux...
2008-7-23 22:56
晨曦的朝阳
Rank: 2
等级:注册会员
帖子:66
积分:866
注册:2008-1-24

就是空间命名污染,已经解决,谢谢2楼。
2008-7-23 23:20
共有 202 人关注过本帖
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.056856 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved