batuman
batuman

Reputation: 7314

Can't access Struct member inside a class

I have a class. Class has a structure _tree_branch_m and a class member function. That structure is a parameter of the member function. Inside the member function, I have an error to access structure members. What is wrong with my structure? The error is Field 'NodeType' could not be resolved.

class CascadeClassifier{


protected:
    typedef  char   *Set_m;
    typedef  struct _tree_branch_m *Tree_m;
    struct _tree_branch_m
    {
            short   NodeType;   /* 0=leaf 1=branch 2=cut 3=subset */
            short   Leaf;       /* most frequent class at this node */
            float   Items,      /* no of items at this node */
                    *ClassDist, /* class distribution of items */
                    Errors;     /* no of errors at this node */
            short   Tested;     /* attribute referenced in test */
            short   Forks;      /* number of branches at this node */
            float   Cut,        /* threshold for continuous attribute */
                    Lower,      /* lower limit of soft threshold */
                    Upper;      /* upper limit ditto */
            Set_m   *Subset;    /* subsets of discrete values  */
            Tree_m  *Branch;    /* Branch[x] = (sub)tree for outcome x */
    };

    virtual void ClassifyCase(Tree_m subtree, float Weight, float *LowClassSum, float *ClassSum);

};

void CascadeClassifier::ClassifyCase(Tree_m Subtree, float Weight, float *LowClassSum, float *ClassSum)
{
     if ( ! Subtree->NodeType )//error
     {

     }

}

Upvotes: 0

Views: 2716

Answers (2)

Igor Tandetnik
Igor Tandetnik

Reputation: 52621

3.3.2/6 The point of declaration of a class first declared in an elaborated-type-specifier is as follows:

— for a declaration of the form

class-key attribute-specifier-seq_opt identifier;

the identifier is declared to be a class-name in the scope that contains the declaration, otherwise

— for an elaborated-type-specifier of the form

class-key identifier

... the identifier is declared in the smallest namespace or block scope that contains the declaration.

When you write typedef struct _tree_branch_m *Tree_m;, struct _tree_branch_m forward-declares a name in the global scope, ::_tree_branch_m. Tree_m is not in any way related to CascadeClassifier::_tree_branch_m.

You need to explicitly forward-declare the member struct:

class CascadeClassifier{
    struct _tree_branch_m;  // forward delcaration
    typedef _tree_branch_m *Tree_m;
    struct _tree_branch_m {...};
};

Upvotes: 3

OmnipotentEntity
OmnipotentEntity

Reputation: 17131

Example in action: http://ideone.com/OxaJVj

You're forward declaring the struct at the same time as assigning it to a typedef. I do not know if this is kosher and I would have to check the standard. But simply separating these two steps makes it work:

typedef  struct _tree_branch_m *Tree_m;

to

struct _tree_branch_m;
typedef _tree_branch_m *Tree_m;

That being said, I would recommend against using leading underscores, because even though you're technically not in violation of the standard, if you were to capitalize the T or put a second _ or move it to the global namespace you'd be in violation of reserved names.

Upvotes: 2

Related Questions