I have this code:

#include <iostream>
using namespace std;
class hello
{
private:
    char array[4][5];
public:
    hello();
    void get_hello(){cout << "From the function: array[2][2] = " << array[2][2] << endl;}
} hi;

hello::hello()
{
    char array[4][5] = 
    {
        " ###",
        "####",
        "####",
        "####"
    };
    cout << "From the constructor: array[2][2] = " << array[2][2] << endl;
    (*this).get_hello();
}

int main()
{
    hi.get_hello();
    cout << "\n" << "Press Enter to end this programm";
    cin.get();
    cout << endl;
    return 0;
}

Output:
>From the constructor: array[2][2] = #
>From the function:array[2][2] =
>From the function:array[2][2] =

I know I can't acces the array because I replaced it with an automatic version in the contructor. What would be the correct syntax to set the class variable that way?
I have tried:

array[4][5] = 
{
    " ###",
    "####",
    "####",
    "####"
};

and

array = 
{
    " ###",
    "####",
    "####",
    "####"
};

but neither of them worked. Can you set an class variable that way or do I have to make an programm that autogenerates code that set the
variable's like:

array[0][0] = ' ';
array[0][1] = '#';

You need to use for loops or strcpy function inside constructors.

You need to use for loops or strcpy function inside constructors.

I'm sorry, I don't get what you mean. I know what loops and strcpy is but I don't see yet what you mean with it.

Maybe something like

#include <iostream>
using namespace std;
class hello
{
private:
    char array[4][5];
public:
    hello();
    void get_hello()
    {
        cout << "From the function: array[2][2] = " << "'" << array[2][2]  << "'" << endl;
    }
} hi;

hello::hello()
{
    for (int i = 0; i < 4; ++i)
    {
        strcpy ( (array[i] ), "####");
    }
        cout << "From the constructor: array[2][2] = " << array[2][2] << endl;
        (*this).get_hello();
}

int main()
{
    hi.get_hello();
    cout << "\n" << "Press Enter to end this programm";
    cin.get();
    cout << endl;
    return 0;
}

Hope it helped, bye.

No, I want to store ascii sprites for an (very) simple game in an object for the sake of data hiding. I have for example this sprite(not made by me):

"      #####        ",
"     #     #       ",
"    #       #      ",
"   #       # #     ",
"   #       # ##    ",
"   #           #   ",
"   #           #   ",
"   #    #    ##    ",
"    #    ## #      ",
"     #     #       ",
"     #######       ",
"     #     #       ",
"     #  #  #       ",
"     ##  # #       ",
"     ##  # #       ",
"     ##  # #       ",
"     #######       ",
"      #  ##        ",
"      ### #        ",
"      #   #        ",
"      #   #        ",
"      ######       ",
"      #     #      ",
"      #     #      ",
"      #######      ",

But if there's no good way of doing it like that :(
Then I have to generate code ;P

Maybe you can do a workaround like this

#include <iostream>
using namespace std;

class Mine
{
    char pattern [6][30] ;

    public:

    Mine ()
    {
      char buffer [6] [30] = {"      #####        ",
                                        "     #     #          ",
                                        "    #       #         ",
                                        "   #       # #       ",
                                        "   #       # ##     ",
                                        "   #               #   "
                                        };

        for (int i = 0; i < 6; ++i)
        {
            strncpy ( pattern [i], buffer [i], 30);
        }

    }

    void display ( )
    {
       for (int i = 0; i < 6; ++i)
       {
           fputs (pattern [i], stdout);
           putchar ('\n');
       }
    }

};

int main (void)
{
    Mine me;
    me.display ();
    return 0;
}

Hope it helped, bye.

I solved it with the following code:

strcpy(array[0]," ###");
strcpy(array[1],"####");
strcpy(array[2],"####");
strcpy(array[3],"####");

Thanks for taking the time to respond and help me further with this.

The code doesn't work because you set the array to be multi-dimensional (e.g. array[5][5]).
You did not set all of the arrays.
The array code should be:

int array[2][2] = {1, 1, 1}
                         {1, 1, 1}
                         {1, 1, 1};
cout << "array[0][2] == 1";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.