0

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
     {

     }

}
7
  • 2
    What error exactly? In which line?
    – Mohit Jain
    Dec 11, 2014 at 5:07
  • If you use the word error in your question, please be kind enough to include it with the question.
    – Mark Hall
    Dec 11, 2014 at 5:08
  • That's not reproducable, provide a MCVE please. Dec 11, 2014 at 5:10
  • @jogojapan, that doesn't matter because by the time he's in the function parameters, he's already in the namespace of the class. That only matters for a leading return type (but not a trailing one, which is why they exist.) Dec 11, 2014 at 5:14
  • Sorry the error message is added. The error is Field 'NodeType' could not be resolved. Thanks
    – batuman
    Dec 11, 2014 at 5:14

2 Answers 2

3

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 {...};
};
1
  • Thanks, I was hunting this down in the standard and you saved me the trouble. So he was in violation of the reserved naming rules. That's very interesting. Dec 11, 2014 at 5:28
2

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.