Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在使用类实例和成员函数时,如何操作主程序中数组内的值?_C++ - Fatal编程技术网

C++ 在使用类实例和成员函数时,如何操作主程序中数组内的值?

C++ 在使用类实例和成员函数时,如何操作主程序中数组内的值?,c++,C++,我想知道如何将我的数组arr[STACK_CAPACITY]放入主程序中,以便它在堆栈中推送和弹出值。目前,我得到一个内存泄漏错误和字符没有得到改变,因为我希望它 #include "stdafx.h" #include <iostream> #include <string> using namespace std; #define STACK_CAPACITY 1000 char arr[STACK_CAPACITY]; int index = 0; clas

我想知道如何将我的数组arr[STACK_CAPACITY]放入主程序中,以便它在堆栈中推送和弹出值。目前,我得到一个内存泄漏错误和字符没有得到改变,因为我希望它

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

#define STACK_CAPACITY 1000

char arr[STACK_CAPACITY];
int index = 0;


class Stack
{

public:
    Stack(); // constructor for a stack
    void push(char c); // adds c to the top of the stack
    char pop(); // removes top element
    char top(); // returns the top element
    bool isEmpty(); // returns true iff the stack is empty
    bool isFull(); // returns true iff the stack is full
    ~Stack(); // destructor for a stack
};

 Stack::Stack() { }

void Stack::push(char c) {
    if (!isFull()) {
        arr[index] = c;
        index++;
    }
    else {
        cout << "Index has exceeded." << "\n";
    }
}

char Stack::pop() {
    char removed;

    if (!isEmpty()) {
        index--;
        removed = arr[index];
    }
    else {
        cout << "Stack can go down any further." << "\n";
    }

    return removed;
}

char Stack::top() {
    return arr[index];
}

bool Stack::isEmpty() {
    bool all_empty;

    if (index == -1) {
        all_empty = true;
    }
    else {
        all_empty = false;
    }
    return all_empty;
}


bool Stack::isFull() {
    bool all_full;

    if (index == 1000) {
        all_full = true;
    }
    else {
        all_full = false;
    }
    return all_full;
}

Stack::~Stack() { }


int main()
{   
    string inp;
    Stack instance;

    cout << "Please enter a string: ";

    while(getline(cin,inp)) {

        if (inp == "^D") {
            break;
        }
        for (int i = 0; i < (int)inp.length(); i++) {
            //cout << inp[i] << "\n";

            arr[i] = inp[i];
            instance.push(arr[i]);
            cout << instance.pop();
        }
        cout << "\n";

        //for (int j = inp.length() - 1; j >= 0; j--) {
        //  cout << inp[j];

        //}
        //cout << "\n";

        cout << "Please enter a string: ";
     }
     return 0;
}
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
#定义堆栈容量1000
char arr[堆栈容量];
int指数=0;
类堆栈
{
公众:
Stack();//堆栈的构造函数
void push(char c);//将c添加到堆栈顶部
char pop();//删除顶部元素
char top();//返回top元素
bool isEmpty();//当堆栈为空时返回true
bool isFull();//当堆栈已满时返回true
~Stack();//堆栈的析构函数
};
堆栈::堆栈(){}
void Stack::push(char c){
如果(!isFull()){
arr[指数]=c;
索引++;
}
否则{
库特
…如何将我的阵列arr[堆栈容量]放置在主程序中

您没有;它不属于
main
程序;它应该是
Stack
类的成员。在这段代码中有许多错误或不明智的地方:

  • arr
    index
    不应在
    main
    中修改;这就是类提供成员函数访问的原因。-
    arr
    应该是
    堆栈的成员
  • 索引
    应该是
    堆栈的成员
  • 索引
    应为无符号数据类型(它同时反映索引和幅值)
  • 索引
    应参考下一项的存储位置,这意味着
    0
    值表示堆栈为空,而
    容量
    值表示堆栈为满
  • main
    应该在循环弹出字符串字符之前将所有字符串字符推入堆栈(这将给您提供您似乎正在寻找的相反顺序)
  • isFull()
    isEmpty()
    top()
    都应该是
    const
    ,因为它们都不需要可变对象来执行
  • pop()
    应返回
    void
    (类似于标准库)
尽可能简单,上述所有应用结果如下:

#include <iostream>
#include <string>

class Stack
{
    static constexpr size_t CAPACITY = 1000;
    char arr[CAPACITY];
    size_t index = 0;

public:
    void push(char c);      // adds c to the top of the stack
    void pop();             // removes top element
    char top() const;       // returns the top element
    bool isEmpty() const;   // returns true iff the stack is empty
    bool isFull() const;    // returns true iff the stack is full
};

void Stack::push(char c)
{
    if (index < CAPACITY)
        arr[index++] = c;
    else
        std::cerr << "idx has exceeded.\n";
}

void Stack::pop()
{
    if (index > 0)
        --index;
    else
        std::cerr << "attempt to pop an empty stack\n";
}

char Stack::top() const
{
    if (index == 0)
    {
        std::cerr << "Attempt to fetch top of empty stack\n";
        return 0;
    }
    return arr[index-1];
}

bool Stack::isEmpty() const
{
    return index == 0;
}

bool Stack::isFull() const
{
    return index == CAPACITY;
}


int main()
{
    Stack instance;
    std::string inp;

    std::cout << "Please enter a string: ";

    while(getline(std::cin,inp) && !inp.empty())
    {
        for (auto x : inp)
            instance.push(x);

        for (; !instance.isEmpty(); instance.pop())
            std::cout << instance.top();
        std::cout << '\n';

        std::cout << "Please enter a string: ";
    }

    return 0;
}
免责声明


当然,标准免责声明也适用:除非这是学术界或一些自我指导的教程,否则容器适配器显然是您自己的首选。

Define“memory leak error”。我发现这里没有发生动态分配。没有任何泄漏。此外,您需要编辑您的问题,并正确缩进和格式化代码。随意的缩进和糟糕的格式使显示的代码几乎无法理解。不要紧,我没有收到泄漏错误。它只是目前没有打印任何内容。有没有出现字母,我想可能是因为字符还没有放在数组中,但我不确定。为什么在类中有全局变量作为成员变量?为什么要在
main
函数中修改数组并使用类成员函数(例如
arr[I]=inp[I]
后跟
instance.push(arr[i])
没有意义)。它没有按相反的顺序打印,我也不太清楚原因。这可能与我的pop函数有关,但我看不出它有任何问题,因此我认为这可能是因为我的数组以及我如何声明它。仅供参考:
删除的
在您的
pop
遇到空堆栈条件时是不确定的,因此,您也应该如此r函数结果返回给调用者。相关的,正如所写的,这不会以相反的顺序输出数据。每次循环迭代你都在推送和弹出。你的堆栈从来没有超过一个元素。非常感谢你的帮助!我想主要的问题是我没有在类中声明数组。我非常迷茫哈哈。