简体   繁体   中英

How Can I Access Method to Method Inside the Same Class?

using namespace std;
class Puzzle
{
public:
    void SetTable() //Is there a better way to do this? (2d array)
    {
        const int four = 4;
        char Table[four][four]=

        {
            {'f', 'k', 's','a'},
            {'l', 'u', 'o','w'},
            {'y', 'o', 'n', 'a'},
            {'x', 't', 'o', 'y'}
        };
    }

    void OutputTable()
    {
        int n=0;
        while (n < 4)
        {
            for (int x = 0; x < 4; x++)
            {
                cout << Table[x][n] << "   "; //error here
            }
            cout << endl;
            cout << endl;
            n++;
        }

    }
};

int main()
{
   Puzzle connect;
   connect.SetTable();
   connect.OutputTable();
   return 0;
}

Is there a better way to set a 2d array inside the class Puzzle? How can I access void SetTable inside void OutputTable? All variables must be inside the class Puzzle. Thanks in advance!

I will advise you to practice more as this you are showing in your question is wrong and there are many ways to implement it and this is the wrong way because the 2D array as you said is not accessible from anywhere else, making it something like hidden and the function does not sets the contents of the 2D array just declares that there is a 2D array inside the function only.

To help you start, do this:

#include <iostream>
using namespace std;
class Puzzle
{
public:
    char Table[4][4] =
    {
        {'f', 'k', 's','a'},
        {'l', 'u', 'o','w'},
        {'y', 'o', 'n', 'a'},
        {'x', 't', 'o', 'y'}
    };
    // This function will be more useful if the `Table` is not public
    void SetTable(int row, int col, char value)
    {
        Table[row][col] = value;
    }

    void OutputTable()
    {
        int n=0;
        while (n < 4)
        {
            for (int x = 0; x < 4; x++)
            {
                cout << Table[x][n] << "   "; //error here
            }
            cout << endl;
            cout << endl;
            n++;
        }

    }
};

int main()
{
   Puzzle connect;
   connect.SetTable(2, 3, 'A');
   connect.OutputTable();
   return 0;
}

Use std::array as a class member variable to realize that.

Your code should look as follows:

using namespace std;
class Puzzle
{
    constexpr int four = 4;
    std::array<std::array<char>,four>four> Table;

public:
    Puzzle() : Table {
            {'f', 'k', 's','a'},
            {'l', 'u', 'o','w'},
            {'y', 'o', 'n', 'a'},
            {'x', 't', 'o', 'y'}
        } {}
    }
    // void SetTable() omit that completely

    void OutputTable()
    {
        for(const auto& row : Table) {
            for(const auto& col : row) {
            {
                cout << col << " ";
            }
            cout << endl;
        }    
    }
};

int main()
{
   Puzzle connect;
   connect.OutputTable();
   return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM