多次元配列bool配列を作成し、bool値を多次元に格納し、多次元配列を出力する関数を含むクラスを作成しています。C++クラスは局所解体に頼っています
これまでのところ、私のクラスは今のところうまく動作します。しかし、クラス関数がタスクを完了するたびに、クラス関数がローカルの多次元配列を作成する必要がないようにしたいと思います。メンバーデータとして作成することができれば理想的ですが、それが可能かどうか、少なくとも私は間違ったことを何度もやっているので、それは不可能だと私に信じさせました。私はまだ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;
}
ありがとうございます。これですべてがわかります。本当にありがとう! – Badlittlepiggy