|
i fixed the problem
because my program is a dll
i actually create an invisible window so i have a window procedure for the popup menu, when the popup menu gets destroyed it calls DestroyWindow to destroy the invisible window.
for some reason when i use a message loop after TrackPopupMenuEx the loop catches the WM_DESTROY message, quits the loop and after that MessageBox works.
it fixes the problem (MessageBox didn't show) but i don't quite understand why ?
here's a snippet of code
item_selected = TrackPopupMenuEx(menu, flags, x, y, hInvisibleWindow, NULL);<br />
<br />
<br />
int result = 0;<br />
<br />
MSG msg;<br />
<br />
while( (result = GetMessage( &msg, NULL, 0, 0 )) != 0)<br />
{ <br />
if (result == -1)<br />
{<br />
}<br />
else<br />
{<br />
TranslateMessage(&msg); <br />
DispatchMessage(&msg); <br />
}<br />
}<br />
<br />
MessageBox(NULL, "Got to the end of showPopupMenu !", "Success!", MB_ICONEXCLAMATION | MB_OK);
code from the WndProc
case WM_UNINITMENUPOPUP:<br />
DestroyWindow(hInvisibleWindow);<br />
break;<br />
case WM_DESTROY:<br />
PostQuitMessage(0);<br />
break;<br />
default:<br />
return DefWindowProc(hwnd, msg, wParam, lParam);
|
|
|
|
|
/*
Visual Studio 2008
Vector is a template class,and there is a class iterator in Vector;
I declare the function operator== as the iterator's friend function
(according to C++Primer,we'd better declare operator== as a friend function)
then how to do that? how to write the friend function operator== ?
the compiler can not deduce T,help me please!
I am a Chinese student,my English is not very good,I hope
you can understand me! ^-^
*/
#include<iostream>
using namespace std;
template<typename T> class Vector;
template<typename T> bool operator==(typename Vector<T>::iterator it1,typename Vector<T>::iterator it2);
template<typename T> class Vector
{
public:
class iterator
{
friend bool operator==(typename Vector<T>::iterator it1,typename Vector<T>::iterator it2);
};
};
template<typename T> bool operator==(typename Vector<T>::iterator it1,typename Vector<T>::iterator it2)
{
return true;
}
int main()
{
Vector<int>::iterator it1,it2;
//cout<<(it1==it2)<<endl;//failed,can not deduce the T,why? help me!
cout<<operator==<int>(it1,it2)<<endl;//success,but not very useful
return 0;
}
|
|
|
|
|
As workaround, you may define the == operator as member of th iterator class:
template<typename T> class Vector
{
public:
class iterator
{
public:
bool operator==(iterator it2)
{
}
};
};
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
This should be fine.
template<typename T>
bool operator==(T it1, T it2)
{
return true;
} Now compiler will be able to infer the type correctly.
wanchao507 wrote: according to C++Primer,we'd better declare operator== as a friend function)
Scott Meyers says, declare operator overload functions as non-member functions and make it friend only if it requires access to internals.
Best wishes,
Navaneeth
|
|
|
|
|
Thanks for Navaneeth.
You inspired Me.
I solved the problem successfully.
A very hard question? friend function again! remember my Question.
I declare the operator== as the class iterator's friend function just because
i want to access its private members.you mentioned that.
I reply the Question too,so others who meet with the same Question can get help!
my answer is just below yours!
Anyway,here I want to Thank You for your help!
|
|
|
|
|
You are welcome. You really don't have to make iterator a class. Iterators can be implemented using simple pointers.
Best wishes,
Navaneeth
|
|
|
|
|
Thanks for Navaneeth.
You inspired Me!
I solved the problem successfully
I hope my source code below can help others who meet with the same problem!
#include<iostream>
using namespace std;
template<typename T> bool operator==(T it1,T it2);
template<typename T> class Vector
{
public:
class iterator
{
friend bool operator==<Vector<T>::iterator>(typename Vector<T>::iterator it1,typename Vector<T>::iterator it2);
public:
iterator(int a=0):value(a){}
private:
int value;
};
};
template<typename T> bool operator==(T it1,T it2)
{
if(it1.value!=it2.value)
{
return false;
}
return true;
}
int main()
{
Vector<int>::iterator it1(0),it2(1);
cout<<(it1==it2)<<endl;
return 0;
}
|
|
|
|
|
Hi ,
May i know how the static variable is stored in memory...
and is it right if i declare static variable inside class but not outside the clas...
|
|
|
|
|
kumar sanghvi wrote: May i know how the static variable is stored in memory...
I'm not sure what you mean by this. The variable is stored in the program's data space and exists for the life of the program.
kumar sanghvi wrote: and is it right if i declare static variable inside class but not outside the clas...
A static declared inside a class is a class variable and can only be accessed through the class name. A static declared outside a class may be accessed directly in a function, module or application depending on where it is declared.
I suggest a re-reading of the C++ documentation on variables for more information.
|
|
|
|
|
Alright, I've ran into a problem that I'm really confused over:
I'm writing a simple mail client. It runs fine on my computer (Windows XP, SP2). However, when ran on a Vista machine, it crashes. Not as in BSOD-crash, it's more like the whole system just freezes over. A friend of mine, who was testing it on her vista machine, she says that everything just freezes (including the mouse) and the few times she had music on,
*the last few times
*i had musci on
*but not this time
*and it like..
*Stutters
*in that spot
I managed to trace it down to here:
MessageBox(NULL,"got here 1","okay",MB_OK);
closesocket(hSocket);
hSocket = NULL;
MessageBox(NULL,"got here 3","okay",MB_OK);
The first msgbox shows, but the second doesn't. (friend of mine isn't exactly computer-savvy, so I put the messageboxs in there so she could test the app).
I'm really stumped by this, because it works fine on XP. What does Vista change that doesn't allow the proper disconnection and closing of a socket?
If it changes anything, this is all happening in a seperate thread, started by a call to AfxBeginThread inside a WH_KEYBOARD_LL callback function. But that shouldn't make a difference, should it?
If any more info is needed, just ask. It's a rather large piece of code, so it's hard for me to describe everything that's going on.
Thanks in advance. 
|
|
|
|
|
Did you try it on more than one Vista machines? Does it crash at the same spot on every vista computers? You mentioned that it is happeneding in a separate thread, so, could it be a synchronization error? I mean, synchronization errors tend to be different depending on a lot of things, for example we had such error(s) that would never pop up on a single core muchine but they would happen almost every time on dual core systems, also they can depend on timings, for example you could have such an error that doesn't pop up at first but if you run winamp and start playing your favourite music then it does because if the computer has more things to do then your process/threads get less CPU time and then somethings that before did not happen simulteniously before will happen now and cause the problem...
Also, are you sure your hSocket is valid?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
|
|
|
|
|
Again, sorry for the vagueness.
No, I haven't tried it on more than 1 Vista machines, and I doubt it's a synchronization error. All the thread does is create an email header and sends it, while the main thread is open for the UI. Aside from the 4 variables passed to the thread at the start, the thread doesn't even use any of the variables used in the main thread.
Yes, I'm sure my hSocket is valid, as I seem to be able to send emails using it.
I'm going to try it on some more vista machines today, thanks for the suggestion. 
|
|
|
|
|
hxhl95 wrote: If it changes anything, this is all happening in a seperate thread, started by a call to AfxBeginThread inside a WH_KEYBOARD_LL callback function. But that shouldn't make a difference, should it?
Introducing multiple threads will always make a difference. Race conditions, sharing of resources, deadlocks, and context switches are just some of the things that come into play above and beyond the normal stuff.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
im develping a Win32 Program with the name """"Add"""" that takes a """string""" as input at command line. The command line argument should be of the following format (commandline argument may be provided from DOS console):
D:\Add\Debug>Add 2,4
The program will extract the integer values from commandline string (e.g. it will extract the values “2” and “4” in above argument) and will add them, finally the result will be displayed in a message box:
wht i did so far
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpCmdLine, int nShowCmd)
{
LPWSTR *szArgList;
int argCount;
szArgList = CommandLineToArgvW(GetCommandLine(), &argCount);
if (szArgList == NULL)
{
MessageBox(NULL, L"Unable to parse command line", L"Error", MB_OK);
return 10;
}
for(int i = 0; i++; i < argCount)
{
MessageBox(NULL, szArgList[i], L"Arglist contents", MB_OK);
}
LocalFree(szArgList);
return 0;
}
|
|
|
|
|
can anyone help me to complete it ,im stuck
thanks
|
|
|
|
|
Is your application compiled with UNICODE support?
Write your WinMain as _tWinMain .
Also you command line must look like Add 2 4 .
Your for loop is wrong.
It must be -
for (int i = 0; i < argCount; ++i)
{
MessageBox(NULL, szArgList[i], L"Arglist contents", MB_OK);
}
|
|
|
|
|
did you mean the fllowing code i tried? having errors
really not getting through !! kindly help me
what i want to have is
------------------------
a Win32 Program with the name “Add” that takes a string as input at command line. The command line argument should be of the following format (commandline argument may be provided from DOS console):
D:\Add\Debug>Add 2,4
The program will extract the integer values from commandline string (e.g. it will extract the values “2” and “4” in above argument) and will add them, finally the result will be displayed in a message box:
-----------------------
what i did
#include "stdafx.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
LPWSTR *szArgList;
int argCount;
szArgList = CommandLineToArgvW(GetCommandLine(), Add 2 4 );
if (szArgList == NULL)
{
MessageBox(NULL, L"Unable to parse command line", L"Error", MB_OK);
return 10;
}
for (int i = 0; i < argCount; ++i)
{
MessageBox(NULL, szArgList[i], L"Answer is", MB_OK);
}
LocalFree(szArgList);
return 0;
}
|
|
|
|
|
Get a book and learn the basics.
Assignment questions are not appreciated here.
|
|
|
|
|
Ok, so what exactly is the problem? Hint: "It doesn't work" does not qualify as an answer.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Hi,
I have successfully implemented serialization of various collection classes even when the data contained CStrings so I'm very familiar with the requirements.
However, I have attempted to serialize a CArray <myclass, myclass&=""> MyCArray from a CPropertyPage class (to save various page defaults) and I noticed that the following serialize overrides are never executed when MyCArray.Serialize(archive) is called and CStrings recovered from the archive are either empty or have a bad pointer. Integers recovered from the archive are correct.
CMyClass : public CObject
{
DECLARE_SERIAL(CMyClass)
public:
void Serialize(CArchive& ar);
....
}
class CMyPage : public CPropertyPage
{
DECLARE_DYNAMIC(CMyPage)
public:
CArray < CMyClass, CMYClass& > MyArray;
.....
}
MyClass::Serialize(CArchive& ar)
{
}
and
template <> void AFXAPI SerializeElements < CMYClass > (.....)
Assuming there no's easy fix, is there another simple method could I use to save a small amount of default data to disk (integers and CStrings). I'm just trying to allow the user to save the various property page settings so they do not have to enter them each time.
Thanks
P.S. What's really strange and really frustrating is that on another property page I have a CArray < CString, CString& > MYStringArray and it serializes correctly! Could CPropertyPages have it's own Serialize function ?
modified on Saturday, November 7, 2009 9:12 PM
|
|
|
|
|
Try changing DECLARE_DYNAMIC(CMyPage) to DECLARE_SERIAL(CMyPage)
|
|
|
|
|
Superman,
DECLARE_SERIAL did not help but I forgot to mention that in the module where I write the archive, I have some test code which immediately reads back the archive. In this case, the CArray is always correct.
It's when the Property page is closed, re-entered and the archive read that the CStrings have bad pointers. The doubles and ints are OK. I also occasionally get some odd errors like "out of memory" or a locked buffer assertion when I exit the property page (but only when the property page has read the archive).
I can seperate the CStrings and ints/doubles into 2 different archives/files since the files are transparent to the users anyhow. As I said, a CArray < CString, CString& > serializes OK.
modified on Sunday, November 8, 2009 5:31 PM
|
|
|
|
|
Hello! I acutally program in C# and chose to start learning C++. I have two questions. One would be if I develop a C++ application using visual c++ 2008, is there a way for it to be converted to run on Mac OS X? Or do you have to write it over in a different IDE?
The main question I have is I was trying to learn from this tutorial I found on www.learncpp.com.
It seems the tutorials were geared more towards Visual C++ 2005 and not 2008 (I am using 2008).
I ran into trouble with the headers. The code I am using is from the tutorial so I am confused as to why it is not working?
I noticed a few things different. In the tutorial it doesn't include "stdafx.h" which it needs to compile. Also when trying to use a header file i get these errors:
Error 1 error LNK2019: unresolved external symbol "int __cdecl add(int,int)" (?add@@YAHHH@Z) referenced in function _main HelloWorld.obj HelloWorld
Error 2 fatal error LNK1120: 1 unresolved externals C:\C++\HelloWorld\Debug\HelloWorld.exe HelloWorld
Now I understand it is having trouble reading the add.h file but I am not sure why?
Here is my code:
HelloWorld.cpp:
#include "stdafx.h"
#include <iostream>
#include "add.h"
int main()
{
using namespace std;
cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
return 0;
}
and here is my add.h:
#ifndef ADD_H
#define ADD_H
int add(int x, int y);
#endif
Any help would be great! Thanks!
|
|
|
|
|
Nevermind after looking at it again I figured out why.
I still have to include
int add(int x, int y)
{
return x + y;
}
in the HelloWorld.cpp file right? The tutorial didn't show this. IF thats why then whats the point of putting that other code in the header file when you could add
int add(int x, int y)
at the top of the HelloWorld.cpp file? Or I'm guessing you would put the whole function in the header file instead of what the tutorial just showed
|
|
|
|
|
The whole build process consists of a compilation process and a linking process.
Including the header is for the compiler to figure out the signature of the function.
The linker needs the actual definition of the function.
|
|
|
|
|