Thread: error C2248: can not access protected member

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    10

    Question error C2248: can not access protected member

    Hello there,

    I'm currently facing a strange, kind of annoying, problem.
    I'm trying to access a protected member of a base class in a derived class.

    Here is my code for the base class:
    Code:
    class Wnd{
    public:
    	Wnd() : m_hWnd(NULL)
    	{}
    	Wnd(HWND hWnd) : m_hWnd(hWnd)
    	{}
    	operator HWND()
    	{
    		return this->m_hWnd;
    	}
    	operator HWND() const
    	{
    		return ((Wnd*)this)->m_hWnd;
    	}
    	void set_text(const char* text)
    	{
    		SendMessage(this->m_hWnd,WM_SETTEXT,(WPARAM)NULL,(LPARAM)text);
    	}
    	const char* get_text()
    	{
    		unsigned int len = SendMessage(this->m_hWnd,WM_GETTEXTLENGTH,(WPARAM)NULL,(LPARAM)NULL);
    		char *tmp = new char[len];
    		SendMessage(this->m_hWnd,WM_GETTEXT,(WPARAM)len,(LPARAM)tmp);
    		return tmp;
    	}
    	void set_visible(BOOL visible)
    	{
    		ShowWindow(this->m_hWnd,(visible)? SW_SHOW : SW_HIDE);
    	}
    	OVERRIDABLE const Wnd& operator =(HWND hWnd)
    	{
    		this->m_hWnd = hWnd;
    		return *this;
    	}
    	OVERRIDABLE const Wnd& operator =(const Wnd& wnd)
    	{
    		if(this == &wnd) return *this;
    		this->m_hWnd = wnd.m_hWnd;
    		return *this;
    	}
    	OVERRIDABLE BOOL operator ==(const Wnd& wnd)
    	{
    		return (this->m_hWnd == wnd.m_hWnd);
    	}
    	OVERRIDABLE BOOL operator ==(HWND hWnd)
    	{
    		return (this->m_hWnd == hWnd);
    	}
    	void set_hwnd(HWND hWnd)
    	{
    		this->m_hWnd = hWnd;
    	}
    	HWND get_hwnd()
    	{
    		return this->m_hWnd;
    	}
    	void set_hwnd(HWND hWnd) const
    	{
    		((Wnd*)this)->m_hWnd = hWnd;
    	}
    	HWND get_hwnd() const
    	{
    		return ((Wnd*)this)->m_hWnd;
    	}
    protected:
    	HWND m_hWnd;
    private:
    };
    And here is my code for a derived class:
    Code:
    class Combobox : public Wnd{
    public:
    	void add_string(const char* string)
    	{
    		SendMessage(*this,CB_ADDSTRING,(WPARAM)NULL,(LPARAM)string);
    	}
    	void delete_string(const char* string)
    	{
    		return this->delete_string(SendMessage(*this,CB_FINDSTRINGEXACT,(WPARAM)NULL,(LPARAM)string));
    	}
    	void delete_string(unsigned int i)
    	{
    		SendMessage(*this,CB_DELETESTRING,(WPARAM)i,(LPARAM)NULL);
    	}
    	char* get_string(unsigned int)
    	{}
    	void clear()
    	{
    		SendMessage(*this,CB_RESETCONTENT,(WPARAM)NULL,(LPARAM)NULL);
    	}
    	unsigned int get_selected_index()
    	{
    		return SendMessage(*this,CB_GETCURSEL,(WPARAM)NULL,(LPARAM)NULL);
    	}
    	char* get_selected_string()
    	{
    		unsigned int len = SendMessage(*this,CB_GETLBTEXTLEN,(WPARAM)NULL,(LPARAM)NULL);
    		char* tmp = new char[len];
    		SendMessage(*this,CB_GETLBTEXT,(WPARAM)len,(LPARAM)tmp);
    		return tmp;
    	}
    	void set_selected_index(unsigned int i)
    	{
    		SendMessage(*this,CB_SETCURSEL,(WPARAM)i,(LPARAM)NULL);
    	}
    	const Combobox& operator=(HWND hWnd)
    	{
    		this->m_hWnd = hWnd;
    		return *this;
    	}
    	const Combobox& operator=(const Wnd& wnd)
    	{
    		if(this == &wnd) return *this;
    		this->m_hWnd = wnd.m_hWnd;
    		return *this;
    	}
    	const Combobox& operator=(const Combobox& wnd)
    	{
    		if(this == &wnd) return *this;
    		this->m_hWnd = wnd.m_hWnd;
    		return *this;
    	}
    protected:
    private:
    };
    The error exists in
    Code:
    const Combobox& operator=(const Wnd& wnd)
    , more specific on the 2nd line:
    Code:
    this->m_hWnd = wnd.m_hWnd;
    The full error says:
    Code:
    error C2248: 'Gui::Wnd::m_hWnd' : cannot access protected member declared in class 'Gui::Wnd'
    Any help would be appreciated

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You're accessing the Wnd directly as if it was another unrelated object type. When you do that you only have access to the public interface. You can only access the protected interface through a Combobox object, which is why the other operator= that takes a Combobox works fine.

    I think the only solution might be to either get rid of that operator= (why would you want that anyway?), expose the m_hWnd publicly somehow (probably a bad idea) or make Combobox a friend.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    10
    Quote Originally Posted by Daved View Post
    You're accessing the Wnd directly as if it was another unrelated object type. When you do that you only have access to the public interface. You can only access the protected interface through a Combobox object, which is why the other operator= that takes a Combobox works fine.

    I think the only solution might be to either get rid of that operator= (why would you want that anyway?), expose the m_hWnd publicly somehow (probably a bad idea) or make Combobox a friend.
    Hello and thank you for the reply.
    I was just going to post that I solved my problem.

    I was able to solve my problem using the following approach:
    Code:
    const Combobox& operator=(const Wnd& wnd)
    	{
    		Wnd::operator=(wnd);
    		return *this;
    	}
    Grtz, Tom.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Get rid of the whole operator = instead. The base class operator = should take over.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. Replies: 12
    Last Post: 01-09-2007, 04:26 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. how do u access a class protected area?
    By CwannaB in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 08:52 AM
  5. how to access "private or protected " in Class
    By johnnypiere in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2002, 02:33 AM