class member access denied

This is a discussion on class member access denied within the C++ Programming forums, part of the General Programming Boards category; Hi, I need some help with this topic... I've got a base clase "layer" like this : class layer { ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    5

    Question class member access denied

    Hi, I need some help with this topic...

    I've got a base clase "layer" like this :

    class layer
    {
    protected:
    int num_inputs;
    int num_outputs;
    float *outputs;
    float *inputs;

    friend network;
    friend kohonen_network;
    public:
    layer();
    virtual ~layer();
    virtual int calc_out()=0;

    };

    two dervide classes of "layer"

    class input_layer : public layer
    {
    private:
    public:
    input_layer(int,int);
    ~input_layer();
    virtual int calc_out();
    };

    class network
    {

    private:
    layer *layer_ptr[MAX_LAYERS];
    int number_of_layers;
    int layer_size[MAX_LAYERS];
    float *buffer;
    fpos_t position;
    unsigned training;

    public:
    network();
    virtual ~network();
    void set_training(const unsigned &);
    unsigned get_training_value();
    void get_layer_info();
    void set_up_network();
    void randomize_weights();
    void update_weights(const float);
    void write_weights(FILE *);
    void read_weights(FILE *);
    void list_weights();
    void write_outputs(FILE *);
    void list_outputs();
    void list_errors();
    void forward_prop();
    void backward_prop(float &);
    float fill_IObuffer(FILE *);
    int set_up_pattern();

    };
    class kohonen_layer: public layer
    {
    protected:
    float *weights;
    int winner_index;
    float win_distance;
    int neighborhood_size;

    friend kohonen_network;

    public:
    kohonen_layer(int,int,int);
    virtual ~kohonen_layer();
    virtual int calc_out();
    void randomize_weights();
    void update_neigh_size(int);
    void update_weights(const float);
    void list_weights();
    void list_outputs();
    float get_win_dist();

    };

    And another class :

    class kohonen_network
    {
    private:

    layer *layer_ptr[2];
    int layer_size[2];
    int neighborhood_size;
    float alpha;

    public:
    kohonen_network();
    virtual ~kohonen_network();
    void get_layer_info(int,int);
    int set_up_network(int);
    void randomize_weights();
    void update_neigh_size(int);
    void update_weights(const float);
    void list_weights();
    void list_outputs();
    void list_structure(CFile *);
    void get_next_vector(CFile *);
    void process_next_pattern();
    float get_win_dist();
    int get_win_index();
    int train_network(CFile *inputfile,int period);
    };

    In kohonen_network class I've declared the member layer_ptr as array of
    layer *.

    When I'm creating an object of this class I do the following :

    layer_ptr[0] = new input_layer(0,layer_size[0]);
    layer_ptr[1] = new
    kohonen_layer(layer_size[0],layer_size[1],neighborhood_size);

    So I've redefined each element with a distinct derived class of layer.

    My problem is : when I'm trying to get the weights member of kohonen_layer
    class thru this statement :
    layer_ptr[1].weights , wich is in a kohonen_network function member (note
    tha kohonen_network is declares as friend so has access to private or
    protectes members )

    I get the following compiler error : error C2039: 'weights' : is not a
    member of 'layer'

    So, I define an array of pointers to a base class, then , assign each
    pointer a derived class, but, I can't acces to member of the derived class ?

    I'm compiling this with a Microsoft C++ Compiler.

    Thanks in advance.

  2. #2
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    For crying out loud!
    Please edit your post and place the code between code tags
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    I get the following compiler error : error C2039: 'weights' : is not a member of 'layer'

    So, I define an array of pointers to a base class, then , assign each pointer a derived class, but, I can't acces to member of the derived class ?
    Thats right. If you know what derived class you have, cast your pointer to that class and access your members of your derived class through this.

    Your compiler doesn't know what baseclass pointer points to which derived classes object. Layer doesn't have a weight, only the derived class has, so calling weight on a layer leads to compiler errors.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-22-2008, 01:07 PM
  2. Replies: 2
    Last Post: 04-19-2008, 01:06 AM
  3. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 07:32 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM

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