Thread: using a class constructor w/ class array

  1. #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.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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;
    };

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    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!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Animation class not working
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 03-02-2005, 06:48 AM
  3. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  4. Replies: 4
    Last Post: 09-12-2001, 02:05 PM

Tags for this Thread