c ++中的重载类函数?

时间:2013-04-10 04:09:44

标签: c++ overloading

我正在尝试重载类下的函数,但我将不同的数据类型传递给函数。传递给它的值可以是intchar,具体取决于输入。但我完全很困惑如何去做。这是我的函数定义和主要函数。

    int main()        
    {
                int pushInt;
                char pushInt;
                cout<<"\n\nEnter Number: ";
                cin >> pushInt;
                mainClassStack.pushNumber(pushInt);
                break;
            }

void stack_class::pushNumber(int numberFunc)
{
    if(head==NULL)
    {
        head = new stack_struct;
        head->number = numberFunc;
        head->next_number = NULL;
        tailPointer=head;
    }
    else
    {
        pointerFunc = new stack_struct;
        pointerFunc->number=numberFunc;
        pointerFunc->next_number=NULL;
        head->next_number=pointerFunc;
        head=pointerFunc;
    }
}


void stack_class::pushNumber(char charFunc)
{

    int a=0;
    a= charFunc;
    if(head==NULL)
    {
        head = new stack_struct;
        head->number = a;
        head->next_number = NULL;
        tailPointer=head;
    }
    else
    {
        pointerFunc = new stack_struct;
        pointerFunc->number=a;
        pointerFunc->next_number=NULL;
        head->next_number=pointerFunc;
        head=pointerFunc;
    }

}

如果您需要我的课程定义,请点击此处

class stack_class
{
private:

    struct stack_struct *head;

public:
    stack_class();//constructor
    ~stack_class();//destructor
    void pushNumber(int number);
    void pushNumber(char number);
    void pushNumber(int *number);
    void popNumber();
    void findNumber();
    void clearStack();
    void sizeFinder();
    void printStack();
    void reverseStack();//Extra Credit
};

stack_class mainClassStack;

stack_struct *pointerFunc,*tailPointer=NULL,*pointerFunc3,*printPointer;

我尝试编译,但是当我尝试通过编译器运行时,程序会冻结。有什么建议我该怎么办?

0 个答案:

没有答案