Thread: how to access pointer to member in nested structure on non class type in c++?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    177

    how to access pointer to member in nested structure on non class type in c++?

    Code:
    typedef struct Node {
    	value_t valueof;
    } Node;
    
    
    
    typedef struct BranchNode {
            
           	struct BranchNode *left_node;
    	struct BranchNode *mid_node;
    	struct BranchNode *right_node;
    	struct Node *left_val;
    	struct Node *right_val;
         } BranchNode;
    
    While I try to access data member in the below NodeAll function, it gives error
    error: error: request for member 'Node' in 'newBranchNode->TreeNode::left_val', which is of non-class type 'DataNode*'
    
    Can someone tell me how to access this nested pointer to member structure for non class type?
    
    BranchNode* NodeAll(BranchNode *root, int val) 
    { 
        BranchNode* newBranchNode;
        BranchNode *BranchNodeptr;
        newBranchNode = newBranchNode;
        newBranchNode->left_node = NULL;
        newBranchNode->right_node = NULL;
        newBranchNode->mid_node = NULL;
        newBranchNode->left_val.Node->valueof;  // error here 
        newBranchNode->right_val.Node->valueof;  
        return newBranchNode; 
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,452
    Since left_val is a pointer you probably need to use pointer notation to access Node. You also don't need that "Node".

    Something like:
    Code:
    newBranchNode->left_val->valueof;

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    177
    Quote Originally Posted by jimblumberg View Post
    Since left_val is a pointer you probably need to use pointer notation to access Node. You also don't need that "Node".

    Something like:
    Code:
    newBranchNode->left_val->valueof;
    Code:
    
    typedef struct Node {
    	value_t valueof;
    } Node;
    
    
    
    typedef struct BranchNode {
            
           	struct BranchNode *left_node;
    	struct BranchNode *mid_node;
    	struct BranchNode *right_node;
    	struct Node *left_val;
    	struct Node *right_val;
         } BranchNode;
    
    after modifying code, it  throws below error
    error: no matching function for call to 'BranchNode::BranchNode()'
    compilation terminated due to -Wfatal-errors.
    
    
    
    BranchNode* NodeAll(BranchNode *root, int val) 
    { 
        BranchNode* newBranchNode;
        BranchNode *BranchNodeptr;
        newBranchNode = new BranchNode; // error here 
        newBranchNode->left_node = NULL;
        newBranchNode->right_node = NULL;
        newBranchNode->mid_node = NULL;
        newBranchNode->left_val->valueof;  
        newBranchNode->right_val->valueof;  
        return newBranchNode; 
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,452
    In future please post the question outside of code blocks.

    That error is telling you that you need a constructor/destructor for your class. The constructor should take care of all of those assignments making NodeAll much simpler.

    By the way lines 30 and 31 are actually not doing anything, perhaps you meant to assign some value to those valueof variables?

    Also since this is C++ you should be using nullptr instead of NULL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-18-2012, 11:17 AM
  2. Can Nested class access parent class's private member?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 08:42 AM
  3. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  4. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  5. Replies: 4
    Last Post: 12-12-2002, 02:32 PM

Tags for this Thread