C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-31-2009, 08:48 PM   #1
Registered User
 
Join Date: Oct 2009
Posts: 1
Unhappy using a class constructor w/ class array

I don't know how to describe this, so i'll give some example code:

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;
}
in this, both nueral_node and layer are going to be treated as data types.

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   Reply With Quote
Old 10-31-2009, 09:34 PM   #2
Guest
 
Sebastiani's Avatar
 
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   Reply With Quote
Old 10-31-2009, 09:36 PM   #3
Webhead
 
Spidey's Avatar
 
Join Date: Jul 2009
Posts: 278
Quote:
can i actually use a pointer on a class? it's been so long since i used classes.
Yes, you can use a pointer on pretty much anything that has a memory address.

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   Reply With Quote
Old 11-01-2009, 06:14 AM   #4
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by Spidey View Post
...and you are treating it as a 2D array, which is a pointer to a pointer...
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 11-01-2009, 07:08 AM   #5
Webhead
 
Spidey's Avatar
 
Join Date: Jul 2009
Posts: 278
Quote:
Originally Posted by Elysia View Post
A 2D array is not a pointer to pointer. It is a pointer to an array.
oops! My Bad, Thats what I meant.
__________________
Spidey out!
Spidey is offline   Reply With Quote
Reply

Tags
arrays, classes

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:15 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22