当前位置:  开发笔记 > 编程语言 > 正文

重载`--`运算符在Mac上的C++中不起作用

如何解决《重载`--`运算符在Mac上的C++中不起作用》经验,为你挑选了1个好方法。

我试图在C++中重载运算符.
我正在创建一个自定义Stack数据结构.

这是我Stack.hheaders目录中:

/*******************************************************************************
 * Stack --                                                                    *
 * This class will just interface a stack of integers.                         *
 * A stack is a linear data structure. Elements are pushed into stack from the *
 * bottom and is popped from the top.                                          *
 *                                                                             *
 * Author -- Aditya R.Singh                                                    *
 * Version -- 1.0                                                              *
 * Since -- 2014-06-24                                                         *
 *******************************************************************************/



#ifndef STACK_H
#define STACK_H



class Stack {

    public:
        Stack();                 // To initiallize stuff.
        void operator>>(int);    // To push elements in the stack.
        int operator--();        // To pop element from the stack.
        void display_stack();    // To display the whole stack.



    private:
        typedef struct stack {

            int data;
            struct stack *top, *previous;
        } node;                           // The structure of the whole stack.
        node *PTR;                        // To point to the top of the stack.
        node *BUFFER;                     // To temporarily store the node to be     freed.
        int data;                         // Data to be popped from the stack.
        int choice;                       // To get choice of the user. 
};



#endif 

我的Stack.cpp文件,我的sources目录是:

/*******************************************************************************
 * Stack --                                                                    *
 * This file contains the implementation of all functions from the class Stack.*
 *                                                                             *
 * Author -- Aditya R.Singh                                                    *
 * Version -- 1.0                                                              *
 * Since -- 2014-06-24                                                         *
 *******************************************************************************/



#include 
#include "../headers/Stack.h"



using namespace std;



Stack::Stack() {

    PTR->top = NULL;
    PTR->previous = NULL;
}



void Stack::operator>>(int data) {

    PTR = (node*)malloc(sizeof(node));
    PTR->previous = PTR->top;
    PTR->top = PTR;
    PTR->data = data;
}    



int Stack::operator--() {

    BUFFER = PTR->top;
    data = BUFFER->data;
    PTR->top = PTR->previous;
    free(BUFFER);
    return data;
}    



void Stack::display_stack() {

    BUFFER = PTR;



    while(BUFFER != NULL) {

        cout << BUFFER->data << endl;
        BUFFER = BUFFER->previous;
    }
}

Main.cppsources目录中:

/*******************************************************************************
 * Main --                                                                     *
 * This program will demonstrate the use of a stack.                           *
 *                                                                             *
 * Author -- Aditya R.Singh                                                    *
 * Version -- 1.0                                                              *
 * Since -- 2014-06-24                                                         *
 *******************************************************************************/



#include  
#include "../headers/Stack.h"



using namespace std;



int get_choice() {

    int choice = 0;



    cout << "*************************************" << endl;
    cout << "************     STACK    ***********" << endl;
    cout << "*************************************" << endl;
    cout << "********** 0> QUIT APP      *********" << endl;
    cout << "********** 1> PUSH          *********" << endl;
    cout << "********** 2> POP           *********" << endl;
    cout << "********** 3> DISPLAY STACK *********" << endl;
    cout << "********** 4> CLEAR CONSOLE *********" << endl;
    cout << "*************************************" << endl;
    cout << endl << "Enter choice = ";
    cin >> choice;



    return choice;
}



int main() {

    Stack stack;
    int choice = -1;
    int data;



    while(choice) {

        choice = get_choice();



        switch(choice) {

            case 1: cout << "Enter number to be pushed = ";
                    cin >> data;
                stack >> data;
                break;

            case 2: cout << "Just pushed " << stack-- << endl;
                break;

            case 3: stack.display_stack();
                    break;

            case 4: system("clear");
                    break;

            default: cout << "Invalid choice!"  << endl;
                     break;                         
        }
    }



    return 0;
}  

headers,sourcesobjects目录在Stack目录中.
我的终端指向Stack目录.
现在我编译了Stack.cpp这样的文件:

gcc -c sources/Stack.cpp -o objects/Stack.o

它编译得很好,并Stack.o在对象目录中生成了该文件.

但是当我Main.cpp像这样编译文件时:

gcc sources/Main -o Stack objects/Stack.o

它给了我以下错误:

Main.cpp:66:43: error: cannot decrement value of type 'Stack'
                    case 2: cout << "Just pushed " << stack-- << endl;

当我试图--(减少)值时,它会发现一些问题stack.但是我把--操作员重载了class Stack.那么可能的问题是什么?

请帮忙.



1> Erbureth say..:

您已经重载了预递减运算符,int operator--();但是您正在使用具有过载签名的后递减运算符.int operator--(int);int参数只是用于区分前缀和后缀版本的标记.

推荐阅读
刘美娥94662
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有