【问题标题】:C++ class relies on local declorationsC++ 类依赖于局部声明
【发布时间】:2016-12-17 14:03:15
【问题描述】:

我正在编写一个类,其中包含将创建多维数组 bool 数组、以多维存储 bool 值以及打印多维数组的函数。

到目前为止,我的课程目前还不错。但是,我希望我的类函数不必为每次我希望类函数完成任务时创建本地多维数组。如果我可以将其创建为成员数据,那将是理想的,但我不确定这是可能的,或者至少我做错了很多次,这让我相信这是不可能的。

旁注我还是 c++ 新手,所以请不要提交包含指针的答案,因为我不知道它们是如何工作的,这只会让我更加困惑。

这是我的代码,感谢您的时间和输入,非常感谢!

//-----------------------------------------------------------------------
//class declorations.
// .h file
#ifndef STACK_H
#define STACK_H
using namespace std;

class stackClass{
    public:  
        const static int index_one = 10;
        const static int index_two = 10;
        //const static bool boolray[index_one][index_two];
        stackClass();
        void set(const int iacross, const int ivert);
        void print();


    private:
        int across;
        int vert;
        const static bool the_array[index_one][index_two];


};

#endif
//----------------------------------------------------------------------------
// class definitions
// .cpp file

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

stackClass :: stackClass(){
bool the_array[index_one][index_two];
for(int across = 0; across < index_one; across++)
    for(int vert = 0; vert < index_two; vert++)  
        the_array[across][vert] = false;
}

void stackClass :: print(){
    bool the_array[index_two][index_one];
    for (int across = 0; across < index_one; across++){ 
        for (int vert = 0; vert < index_two; vert++){
                if(the_array[across][vert] == true){
                    cout << "*";
                }
                else{
                    cout << " ";
                }

        }   
cout << endl;
    }
    }

void stackClass :: set(int iacross, int ivert){
iacross=across;
ivert=vert;
}
//--------------------------------------------------
// clinet program for testing
// .cpp file
#include<iostream>
#include "stack.h"
#include <cstdlib>
using namespace std;

int main(){

    stackClass obj1;
    for (int count = 0; count < 5; count++) { 
    obj1.set(rand()%20, rand()%20);
    }
    obj1.print();

return 0;
}

【问题讨论】:

    标签: c++ arrays function class boolean


    【解决方案1】:

    您的代码存在一些问题。首先,您的 set 函数没有做任何事情。您正在为函数的参数赋值。您的意思是改用across=iacross;(对于 ivert 也是如此)?

    接下来,您的构造函数也没有做任何有用的事情。它创建一个本地数组并对其进行初始化,但是一旦该函数结束,您的本地数组就会消失。

    然后您的print 函数创建一个新的本地数组,不对其进行初始化,然后从中读取值。所以它只是从内存中打印垃圾。

    如果我理解正确,这就是你想要做的。你有一个私人成员the_array,你想使用它。首先,它不应该是const,也可能不是static(不知道为什么你把它设为静态,但这取决于你)。然后在你的构造函数中,去掉这条线

    bool the_array[index_one][index_two];
    

    这样,其余代码将初始化您的成员 the_array 的值。此外,在您的打印功能中,去掉同一行,以便您可以从成员数组中读取。最后,在您的 set 函数中,我最好的猜测是您想要这样做:

    the_array[iacross][ivert] = true;
    

    没有别的了。

    【讨论】:

    • 非常感谢,这让我明白了一切。非常感谢您的意见!
    【解决方案2】:

    如果您希望 the_array 成为成员变量,则应在不使用 static 关键字的情况下声明它。

    即替换

    const static bool the_array[index_one][index_two];

    bool the_array[index_one][index_two];

    stack.h

    此外,正如@grigor 所提到的,您的代码有一些错误,可能并没有像您预期的那样正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      相关资源
      最近更新 更多