Dec 6, 2012 at 4:56pm UTC
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 Dec 6, 2012 at 10:53pm UTC
Dec 6, 2012 at 5:10pm UTC
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 Dec 6, 2012 at 5:12pm UTC
Dec 6, 2012 at 5:24pm UTC
that doesn't work because i dont know how to install boost or c11, my compiler bit me last time i tried.
Dec 6, 2012 at 5:44pm UTC
i will suuss out how to do it with vectors
Dec 6, 2012 at 6:14pm UTC
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 Dec 6, 2012 at 6:15pm UTC
Dec 6, 2012 at 6:45pm UTC
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])
Dec 6, 2012 at 10:57pm UTC
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 Dec 6, 2012 at 11:18pm UTC
Dec 7, 2012 at 10:31am UTC
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 Dec 7, 2012 at 10:36am UTC
Dec 7, 2012 at 6:10pm UTC
line 28 and 29 both have 'char_arr' constructifiers, why is this?
Dec 7, 2012 at 11:43pm UTC
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
Dec 10, 2012 at 8:36pm UTC
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){};
Dec 10, 2012 at 8:38pm UTC
wait its a prototype not a constructor :P
Dec 10, 2012 at 8:42pm UTC
Why when i remove line 28 out of fun2code's code does it not work, what is it for?
Dec 10, 2012 at 8:45pm UTC
oh thats a prototype and not a constructor too :P