![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 1
| Code: class neural_node{
public:
nueral_node(int size){
node_size=size;
}
private:
int node_size;
}
class layer{
public:
layer(int node_size, int number_of_nodes){
layer_nodes=new neural_node[node_size][number_of_nodes];
}
private:
neural_node* layer_nodes;
}
my question is this: how can I use the neural_node constructor for the array? any help will be greatly appreciated! p.s. can i actually use a pointer on a class? it's been so long since i used classes. |
| matt000r000 is offline | |
| | #2 |
| Guest Join Date: Aug 2001
Posts: 4,923
| It isn't clear why you are trying to allocate a two-dimensional array, so I'm assuming that's just a typo. Anyway, you can't declare a variable-length array of objects that don't have a default constructor. Moreover, you shouldn't be using 'new' to allocate *any* array for that matter (quite error prone). The best solution would be to use an std::vector: Code: class neural_node
{
public:
neural_node(int size){
node_size=size;
}
private:
int node_size;
};
class layer {
public:
layer(int node_size, int number_of_nodes)
: layer_nodes( number_of_nodes, neural_node( node_size ) ) {
// ...similar to...
layer_nodes.resize( number_of_nodes, neural_node( node_size ) );
}
private:
std::vector< neural_node > layer_nodes;
};
|
| Sebastiani is offline | |
| | #3 | |
| Webhead Join Date: Jul 2009
Posts: 278
| Quote:
Your code has a lot of mistakes, layer_nodes is a pointer to neural node, and you are treating it as a 2D array, which is a pointer to a pointer. Also, when creating a dynamic array, the only constructor you can call is the default constructor(which must explicitly be present). Also, when allocating dynamic memory, make sure you free it once your done. Here is another way you can achieve what you were trying to do.. Code: class neural_node{
public:
neural_node(){ }
void set_size(int size) {
node_size = size;
}
private:
int node_size;
};
class layer{
public:
layer(int node_size, int number_of_nodes){
layer_nodes = new neural_node[number_of_nodes];
for(int i = 0; i < number_of_nodes; ++i)
layer_nodes[i].set_size(node_size);
}
~layer()
{
delete[] layer_nodes;
}
private:
neural_node* layer_nodes;
};
__________________ Spidey out! | |
| Spidey is offline | |
| | #4 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| A 2D array is not a pointer to pointer. It is a pointer to an array.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #5 |
| Webhead Join Date: Jul 2009
Posts: 278
| oops! My Bad, Thats what I meant.
__________________ Spidey out! |
| Spidey is offline | |
![]() |
| Tags |
| arrays, classes |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Message class ** Need help befor 12am tonight** | TransformedBG | C++ Programming | 1 | 11-29-2006 11:03 PM |
| Animation class not working | Bubba | Game Programming | 5 | 03-02-2005 06:48 AM |
| Abstract class problem | VanJay011379 | C++ Programming | 9 | 07-31-2002 01:30 PM |
| gcc problem | bjdea1 | Linux Programming | 13 | 04-29-2002 06:51 PM |
| Array of pointers for dynamically allocating class instances | sh0x | C++ Programming | 4 | 09-12-2001 02:05 PM |