Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'>>' : operator has no effect; expected operator with side-effect

Status
Not open for further replies.

mikelauwrie

Programmer
May 26, 2004
14
GB
Ok I've got new problems!
I'm trying to create a context menu through following a tutorial out of a book (in Visual C++). When writing the code for the function I get two warnings:

warning C4552: '>>' : operator has no effect; expected operator with side-effect

warning C4700: local variable 'm_lMenu' used without having been initialized

The full code is in the post below:
 
void CMenusDlg::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: Add your message handler code here
//declare local variables
CMenu *m_lMenu; //a pointer to the menu
CPoint m_pPoint; //a copy of the mouse position

//copy the mouse position to a local variable
m_pPoint = point;
//convert the position to a screen position
ClientToScreen(&m_pPoint);
//get a pointer to the window menu
m_lMenu - GetMenu();
//get a pointer to the first submenu
m_lMenu = m_lMenu->GetSubMenu(0);
//show the popup menu
m_lMenu->TrackPopupMenu(TPM_CENTERALIGN + TPM_LEFTBUTTON,
m_pPoint.x, m_pPoint.y, this, NULL);
}



Does anyone know what the problem is here? My "-" symbols look slighter longer than they are in the book, but I can't see anything else that it could be.

Also when it says that 'm_lMenu' has not been initialized, i thought that the bit at the top did that:CMenu *m_lMenu;

Any guidance would be appreciated

Mike Lauwrie
 
> i thought that the bit at the top did that:CMenu *m_lMenu;
That's a declaration, not an initialisation

> Does anyone know what the problem is here?
Yeah, both problems are on the same line
Code:
 m_lMenu - GetMenu();
1. expression without side effect
you're just subtracting one thing from another, but you don't store the result anywhere (that's a side effect)
2. Use prior to initialisation
well you're using m_lmenu, but you didn't initialise it.

My guess is, you meant
Code:
 m_lMenu = GetMenu();
This initialises it, restores the side effect and is consistent with the comment which precedes it.

--
 
Here's what an OnContextMenu "normally" looks like

Code:
void SomeClass::OnContextMenu(CWnd* pWnd, CPoint point) 
{
	CMenu allContextMenus;
	allContextMenus.LoadMenu(IDR_CONTEXTMENUS);

	CMenu* menu = allContextMenus.GetSubMenu(0);
	ASSERT(menu!=0);

	menu->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON, point.x, point.y, this);

}

/Per
[sub]
"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."[/sub]
 
Right well I tried using your suggested replacement of code - Salem.

And it compiles ok and runs. However the context menu doesn't appear near the cursor. It's always off the window, however it does move in relation to the cursor so perhaps it's supposed to be a bit lame!

If I try using Perfnurt's code I get the following error:

error C2065: 'IDR_CONTEXTMENUS' : undeclared identifier

However I can understand that it is due to me not having declared something, (although I'm not sure how to remedy it)!

By the way the code that I originally posted is correct according to the book so.... Don't by "Sams teach yourself VC++ in 21 days"!

Incidentally on irritating thing about it is that it doesn't explain the language in enough detail - it just tells you to do stuff in a WYSIWYG type fashion.

Can anyone recomend any good C++ books?


Thanks for you help everyone


Mike
 
>However I can understand that it is due to me not having declared something

Yes, I assumed you hade actually created a menu (in the resourceeditor) holding the contextmenu.

Don't take the IDR_CONTEXTMENUS literally (though I think it's a good name for a resource ID of a menu with context menus), if you have some other ID identifying your menu-with-context-menus use that one by all means.

/Per
[sub]
"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top