none
a nonstatic member reference must be relative to a specific object

    Question

  • hi,all, I have the following code:

    MSXML2::IXMLDOMNodeListPtr pLeafNodesList;
    MSXML2::IXMLDOMElementPtr m_pDocRoot;
    
    void CGATrainingDlg::GetLeafNodesRef(MSXML2::IXMLDOMElementPtr node, MSXML2::IXMLDOMNodeListPtr &pDOMNodeList)
    {
    	pDOMNodeList = node ->selectNodes(".//*[not(./*)]");
    }
    
    
    GetLeafNodesRef(m_pDocRoot, pLeafNodesList);

    but it generates a error on the function call:

    'a nonstatic member reference must be relative to a specific object', pointing at 

    m_pDocRoot
    Friday, December 13, 2013 11:35 PM

Answers

  • hi,all, I have the following code:

    MSXML2::IXMLDOMNodeListPtr pLeafNodesList;
    MSXML2::IXMLDOMElementPtr m_pDocRoot;
    
    void CGATrainingDlg::GetLeafNodesRef(MSXML2::IXMLDOMElementPtr node, MSXML2::IXMLDOMNodeListPtr &pDOMNodeList)
    {
    	pDOMNodeList = node ->selectNodes(".//*[not(./*)]");
    }
    
    
    GetLeafNodesRef(m_pDocRoot, pLeafNodesList);

    but it generates a error on the function call:

    'a nonstatic member reference must be relative to a specific object', pointing at 

    m_pDocRoot

    Where is this call to GetLeafNodesRef() located? It needs to be in a non-static member function of CGATrainingDlg.

    David Wilkinson | Visual C++ MVP

    Saturday, December 14, 2013 10:38 PM

All replies

  • Usually caused by trying to reference a non-static class member via the class definition.
    Such members don't actually exist in memory except as members of an instantiated object
    of the class. References to non-static class members must be via an *object* of the class,
    not the class definition itself.

    By contrast, static members of a class exist from the time of the class definition and
    only one such member exists regardless of how many instances of the class are created.

    - Wayne
    Saturday, December 14, 2013 3:13 AM
  • For examples with explanations see:

    error:a nonstatic member reference must be relative to a specific object
    http://stackoverflow.com/questions/15691115/errora-nonstatic-member-reference-must-be-relative-to-a-specific-object

    Error: a nonstatic member reference must be relative to a specific object
    http://www.cplusplus.com/forum/beginner/40377/

    - Wayne
    Saturday, December 14, 2013 3:29 AM
  • this is very strange that in my other code that I also use the same code, but don't have the static member reference must be relative to a specific object' problem. The code is like:

    //in header
    MSXML2::IXMLDOMNodePtr m_pDataRegion;
    MSXML2::IXMLDOMNodeListPtr m_pLeafNodesList;
    
    //in cpp file
    //line 1532
    void CXMLDOMFromVCDlg::GetLeafNodesRef(MSXML2::IXMLDOMNodePtr node, MSXML2::IXMLDOMNodeListPtr &pDOMNodeList)
    {
    	pDOMNodeList = node ->selectNodes(".//*[not(./*)]");
    }
    
    //line 2541
    GetLeafNodesRef(m_pDataRegion, m_pLeafNodesList);

    Saturday, December 14, 2013 9:15 PM
  • In what function is line 2541?  Does the cpp file in question define only member functions?
    Saturday, December 14, 2013 9:54 PM
  • it is like:
    //line 2512
    void CXMLDOMFromVCDlg::OnBnClickedStep4Button4()
    { //other code
    
    GetLeafNodesRef(m_pDataRegion, m_pLeafNodesList);
    
    //other code
    }
    
    the cpp file in question only define member functions.
    Saturday, December 14, 2013 10:08 PM
  • hi,all, I have the following code:

    MSXML2::IXMLDOMNodeListPtr pLeafNodesList;
    MSXML2::IXMLDOMElementPtr m_pDocRoot;
    
    void CGATrainingDlg::GetLeafNodesRef(MSXML2::IXMLDOMElementPtr node, MSXML2::IXMLDOMNodeListPtr &pDOMNodeList)
    {
    	pDOMNodeList = node ->selectNodes(".//*[not(./*)]");
    }
    
    
    GetLeafNodesRef(m_pDocRoot, pLeafNodesList);

    but it generates a error on the function call:

    'a nonstatic member reference must be relative to a specific object', pointing at 

    m_pDocRoot

    Where is this call to GetLeafNodesRef() located? It needs to be in a non-static member function of CGATrainingDlg.

    David Wilkinson | Visual C++ MVP

    Saturday, December 14, 2013 10:38 PM
  • I'm  guessing that m_pDocRoot is not a member variable. Can you confirm?
    Saturday, December 14, 2013 10:38 PM