how do i initialize this node push_front function?

See this sexy little snippet, im trying to initialize this;node (char arr [6][6],node *link):arr[6][6](arr[6][6]),link (link) {}

but theres a flaw, how do i declare this properly?





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <string>
#include <iostream>
#include <vector>
class mylist
{
public:
struct node
{
    node *link;
    char arr [6][6];
    void set_arr (int num1,int num2,char x)
    {
        arr [num1][num2] = x;
    }
    char get_arr ( int num1, int num2)
    {
        return arr [num1][num2];
    }
    node () : link(NULL) {}
    node (char arr [6][6],node *link):arr[6][6](arr[6][6]),link (link) {}
};
mylist ():front (NULL){}
mylist::mylist push_back ();
private:
node *front;
};

void mylist::mylist push_back (char arr [6][6])
{
node* = new node (arr [6][6],NULL);
}

int main ()
{


return 0;
}


If you can think of a more efficient way to do what im doing please show me, i know some can.
Last edited on
That's just not how C-style arrays work. You could initialize one in a member init list (since c++11), but not from a pointer to a row that you're sending to the constructor.

How about you use a C++ array?

1
2
3
4
5
6
7
8
9
10
11
12
typedef std::array<char, 6> row_t;
typedef std::array<row_t, 6> array_t;

struct node
{   
    array_t arr;
    node *link;

    node (const array_t& arr, node* link)
    : arr(arr),
      link (link) {}
};  
Last edited on
that doesn't work because i dont know how to install boost or c11, my compiler bit me last time i tried.
i will suuss out how to do it with vectors
kay why is it expecting an initializer before 'push_back' what is it about member selection initializers and all that jazz im failing to grasp, please help can you show me how to do what im trying to do??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <string>
#include <iostream>
#include <vector>
class mylist
{
public:
struct node
{
    node *link;
    char arr[6][6];
    void set_arr (int num1,int num2,char x)
    {
        arr [num1][num2] = x;
    }
    char get_arr ( int num1, int num2)
    {
        return arr [num1][num2];
    }
    node () : link(NULL) {}

};
mylist ():front (NULL){}
mylist::mylist push_back ();
private:
node *front;
};

void mylist::mylist push_back (char arr [6][6])//why did you expect an initializer?
{
//grr
}
int main ()
{
return 0;
}
Last edited on
I guess you want to change line 23 to
void push_back (char arr [6][6]);
and line 28 to
void mylist::push_back (char arr [6][6])
ooh yeah...fanks.
new problem simlar problem (maybe i should review something), how do i initialize this??

1
2
3
4
5
void mylist::front_back (char arr [6][6])
{
front = node* new node (arr[6][6]);
return;
}
bump :D how i do this...front=node* new node = (char array [6][6])? i get the error

C:\Users\user\Documents\C++ folder\Untitled2.CPP|28|error: no 'void mylist::front_back(char (*)[6])' member function declared in class 'mylist'|
||=== Build finished: 1 errors, 0 warnings ===|

if i allready declared it how can i use it

AAAAARGHH!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <string>
#include <iostream>
#include <vector>
class mylist
{
public:
    struct node
    {
        node *link;
        char arr[6][6];
        void set_arr (int num1,int num2,char x)
        {
            arr [num1][num2] = x;
        }
        char get_arr ( int num1, int num2)
        {
            return arr [num1][num2];
        }
        node () : link(NULL) {}

    };
    mylist ():front (NULL) {}
    void push_front (char arr[6][6]);
private:
    node *front;
};

void mylist::push_front (char arr [6][6])
{

   front =  new node ( arr[6][6]);
   return;

}



int main ()
{
    return 0;
}


EDIT: kept submitting errors :/
Last edited on
closed account (D80DSL3A)
You need to initialize your arrays by copying every element.

I suggest this constructor:
1
2
3
4
5
6
node( char Arr[6][6], node* Link ) : link(Link)
{
    for( unsigned i=0; i<6; ++i)
        for( unsigned j=0; j<6; ++j)
            arr[i][j] = Arr[i][j];
}


I'm not sure I should encourage your wild endeavors with lists here.

But what the heck, you may learn something while having fun with it.

The following works and does about what I think you are trying to do here, just copy, paste, build and run:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
using namespace std;

class mylist
{
public:
    struct node
    {
        node *link;
        char arr[6][6];
        void set_arr (int num1,int num2,char x)
        {
            arr [num1][num2] = x;
        }
        void show_arr (void)// a function to display the node array to cout
        {
            for( unsigned i=0; i<6; ++i)
            {
                for( unsigned j=0; j<6; ++j) cout << arr[i][j];
                cout << endl;
            }
        }
        char get_arr ( int num1, int num2)
        {
            return arr [num1][num2];
        }
        node () : link(NULL) {}
        node ( char Arr[6][6] );
        node ( char Arr[6][6], node* Link );

    };
    mylist ():front (NULL) {}
    void push_front (char Arr[6][6]);
    void show(void);
private:
    node *front;
};

mylist::node::node( char Arr[6][6] ) : link(NULL)
{
    for( unsigned i=0; i<6; ++i)
        for( unsigned j=0; j<6; ++j)
            arr[i][j] = Arr[i][j];
}

mylist::node::node( char Arr[6][6], node* Link ) : link(Link)
{
    for( unsigned i=0; i<6; ++i)
        for( unsigned j=0; j<6; ++j)
            arr[i][j] = Arr[i][j];
}

void mylist::show(void)// show entire list
{
    node* iter = front;
    while( iter )
    {
        iter->show_arr();
        cout << endl;
        iter = iter->link;
    }
}

void mylist::push_front (char Arr [6][6])
{
   // this should work now with the new ctor in use here.
   front =  new node ( Arr, front);
   return;

}

int main()
{
    char arr_1[6][6], arr_2[6][6], arr_3[6][6];

    for( unsigned i=0; i<6; ++i)
        for( unsigned j=0; j<6; ++j)
        {
            arr_1[i][j] = 'a';
            arr_2[i][j] = 'b';
            arr_3[i][j] = 'c';
        }

    mylist L;
    L.push_front( arr_1 );// 6x6 array of a's
    L.push_front( arr_2 );// 6x6 array of b's
    L.push_front( arr_3 );// 6x6 array of c's

    L.show();

    cout << endl;
    return 0;
}

Output:

cccccc
cccccc
cccccc
cccccc
cccccc

bbbbbb
bbbbbb
bbbbbb
bbbbbb
bbbbbb
bbbbbb

aaaaaa
aaaaaa
aaaaaa
aaaaaa
aaaaaa
aaaaaa
yeah :D ihave learnt new stuff

1
2
3
4
5
 mylist::node::node( char Arr[6][6] ) : link(NULL)
{
    for( unsigned i=0; i<6; ++i)
        for( unsigned j=0; j<6; ++j)
            arr[i][j] = Arr[i][j];


i think i didnt realize how much their could be to constructors when i first learnt them...go back and revise, at the yellow belt stage of c++ you start to see errors in your old ways
Last edited on
line 28 and 29 both have 'char_arr' constructifiers, why is this?
POW
closed account (D80DSL3A)
There don't really need to be both of those constructors.
Keep just the one taking 2 arguments. It is used in push_back.
1
2
3
4
5
6
mylist::node::node( char Arr[6][6], node* Link ) : link(Link)
{
    for( unsigned i=0; i<6; ++i)
        for( unsigned j=0; j<6; ++j)
            arr[i][j] = Arr[i][j];
}

The other one isn't needed.
ah okay you done that in an earlier example i thought i didnt understand something, i got to remember if it dont look complicated and it doesnt make sense it could be wrong or unessisary, i soent a lot of time trying to fix the wrong things, guess i should use the debugger rather than watching build
so i was wondering, see line 27,28,29 could i have just wrote this line instead?
node ( char Arr[6][6], node* Link ):link(NULL){};
wait its a prototype not a constructor :P
Why when i remove line 28 out of fun2code's code does it not work, what is it for?
oh thats a prototype and not a constructor too :P
Topic archived. No new replies allowed.