Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.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++_Arrays_Stack - Fatal编程技术网

C++ 无法将值分配给堆栈

C++ 无法将值分配给堆栈,c++,arrays,stack,C++,Arrays,Stack,下面是我的堆栈实现的代码 class stack { private: int top; int arr[10]; public: Stack() { top = -1; for(int i=0; i < 10; i++) { arr[i] = 0; } re

下面是我的堆栈实现的代码

class stack
{
    private:
        int top;
        int arr[10];

    public:
        Stack()
        {
            top = -1;
            for(int i=0; i < 10; i++)
            {
                arr[i] = 0;
            }
            return 0;
        }
        //further code below

但是我在Stack()构造函数中为arr[10]的所有值指定了0。
为什么会发生这种情况?

修复键入错误并删除返回“解决”了问题。但您的输出根本不可复制:

#include <iostream>

class Stack
{
    private:
        int top;
        int arr[10];

    public:
        Stack()
        {
            top = -1;
            for(int i=0; i < 10; i++)
            {
                arr[i] = 0;
            }
        }
        //further code below

        void disp()
    {
        for (int i=9; i >= 0; i--)
        {
            std::cout << arr[i] <<  std::endl;
        }
    }
};

int main()
{
    Stack s;
    s.disp();
}
#包括
类堆栈
{
私人:
int top;
int-arr[10];
公众:
堆栈()
{
top=-1;
对于(int i=0;i<10;i++)
{
arr[i]=0;
}
}
//进一步代码如下
void disp()
{
对于(int i=9;i>=0;i--)
{

std::cout我运行了您的代码,如下所示:

#include <iostream>

class Stack
{
private:
    int top;
    int arr[10];

public:
    Stack()
    {
        top = -1;
        for(int i = 0; i < 10; ++i)
        {
            arr[i] = 0;
        }
        // return 0; NOTE: constructor could not return a value
    }
    
    void disp()
    {
        for (int i = 9; i >= 0; --i)
        {
            std::cout << arr[i] << std::endl;
        }
    }
};

int main()
{
    Stack s1;
    s1.disp();
    return 0;
}


我真的不知道你在问什么。

代码不能像现在这样编译。类名和构造函数名不匹配,构造函数也在返回值?Stack():top(-1),arr({0})看起来不像这样乱七八糟。提示:return在构造函数中包含值不是一件事。Stack()<代码>不是<代码>类堆栈的构造函数。C++中的大写化问题。此代码不应编译而不出错或至少警告。
#include <iostream>

class Stack
{
    private:
        int top;
        int arr[10];

    public:
        Stack()
        {
            top = -1;
            for(int i=0; i < 10; i++)
            {
                arr[i] = 0;
            }
        }
        //further code below

        void disp()
    {
        for (int i=9; i >= 0; i--)
        {
            std::cout << arr[i] <<  std::endl;
        }
    }
};

int main()
{
    Stack s;
    s.disp();
}
#include <iostream>

class Stack
{
private:
    int top;
    int arr[10];

public:
    Stack()
    {
        top = -1;
        for(int i = 0; i < 10; ++i)
        {
            arr[i] = 0;
        }
        // return 0; NOTE: constructor could not return a value
    }
    
    void disp()
    {
        for (int i = 9; i >= 0; --i)
        {
            std::cout << arr[i] << std::endl;
        }
    }
};

int main()
{
    Stack s1;
    s1.disp();
    return 0;
}

$ ./a.exe
0
0
0
0
0
0
0
0
0
0