我先自己写好了Stack.cpp和Stack.h两个文件,在头文件中定义了类,并声明了成员函数,然后建立了一个WIN32 APP 将我自己的两个文件加入到这个工程中,在Class View中出现了我所定义的类,以及相应的成员函数,但是就是不能通过双击Class View中的函数名称来到达函数定义处,总是弹出一个对话框"Can not find the defination"implementation"of this function"请问这是为什么?

解决方案 »

  1.   

    //函数的声明在这里
    //Stack.cpp#include "CSqstack.h"
    //CSqStack();          //初始化一个空栈     构造函数 
    template <class type>CSqStack<type>::CSqStack()
    {
        base = (type *)malloc( SATCK_INIT_SIZE * sizeof( type ) );
        if(base ==  NULL)               //内存分配失败 
            exit(NOT_ENOUGH_MEMORY);    //这里在WIN底下不能运行 
        top = base;
        stacksize = SATCK_INIT_SIZE;
    }
    //    ~CSqStack();         //销毁一个栈       析构函数 
    template <class type> CSqStack<type>::~CSqStack()
    {
        free( base );
    }
    //    int ClearStack();        //将栈清空 成功后返回OK 失败后ERROR
    template <class type> int CSqStack<type>::ClearStack()
    {
        top = base;
        return OK;
    } //    int StackEmpty();        //判断栈是否为空 
    template <class type> int CSqStack<type>::StackEmpty()
    {
        if( top == base)    //栈空 
            return( STACK_EMPTY);
        else                //栈不空 
            return( STACK_NOT_EMPTY);
    }//    int StackLength();        //返回栈当前拥有元素的个数
    template <class type> int CSqStack<type>::StackLength()
    {
        int count = 0;
        type current;
        current = top;
        while( current != base )
        {
            count++;
            current--;    //指向下一个元素 
        }
        return(count);
    }
    //    type GetTop();            //获得栈顶元素 此处有一个问题,如果type为类变量,则此type必须重载=运算符 
    template <class type> type CSqStack<type>::GetTop()
    {
        type current;
        current = top;
        current--;
        return(*current);
    }
    //    int Push();            //压栈 有可能返回OK,或者内存分配失败,程序直接退出
    template <class type> int CSqStack<type>::Push(type elem)    //参数:elem 需要压入的量 
    {
        if((top - base) >= stacksize)   //栈满,追加空间
        {
            base = (type *)realloc(base , (stacksize + STACKINCREMENT) * sizeof(type));
            if(!base)        //存储分配失败
                exit(NOT_ENOUGH_MEMORY);
            top = base + stacksize;
            stacksize += STACKINCREMENT;
        }
        *(top++) = e;
        return OK;

    //    int Pop(type &elem);             //出栈   参数为传指针调用,用于返回弹出的值 
    template <class type> int CSqStack<type>::Pop(type &elem)                //成功后返回OK,失败后返回STACK_EMPTY 
    {
        if(top = base)  //栈为空,无法出栈 
            return(STACK_EMPTY);
        elem = *(--top);
        return OK;
    }//    int StackTraverse(int *(visit)(type elem));     //用VISIT方法对所有元素进行访问 
    template <class type> int CSqStack<type>::StackTraverse(int *(visit)(type elem))  //成功访问所有元素后返回OK 
    {
        type *current;
        current = top;
        while(current != base)
        {
            visit(*current);
            current--;
        }
        return OK;
    }
      

  2.   

    //Stack.h
    //类的声明在这里#define STACK_INIT_SIZE 100    //堆栈空间分配的初始大小 
    #define STACKINCREMENT 50  //堆栈的空间分配增量 #define STACK_OVERFLOW 0x03    //溢出错 
    #define STACK_EMPYT 0x04       //栈空错
    #define NOT_ENOUGH_MEMORY 0x05  //可供分配的内存不足 
    #define STACK_EMPTY 0x06        //栈为空
    #define STACK_NOT_EMPTY 0x07    //栈不为空 template <class type>class CSqStack
    {
    private:
        type *base,*top;   //栈顶和栈底指针
        int stacksize;    //栈的当前总容量 
        
    public:
        CSqStack();          //初始化一个空栈
        ~CSqStack();         //销毁一个栈
        int ClearStack();        //将栈清空
        int StackEmpty();        //判断栈是否为空 
        int StackLength();        //返回栈当前拥有元素的个数
        type GetTop();            //获得栈顶元素 
        int Push(type elem);            //压栈 
        int Pop(type &elem);            //出栈 
        int StackTraverse(int *(visit)(type elem));     //用VISIT方法对所有元素进行访问 
    };