|
This isn't specific to visual C++, but hopefully someone can help me anyways
I have a vector full of objects... I wish to sort this vector by the return value of one of the object's methods.
IE
----
vector<dog> dogs;
//load vector with random dogs...
----
I wish to sort the dogs by their age, int dog.get_age();
Is there a simple way to do this?
Thanks!
Greg
|
|
|
|
|
You could simply use a map with the key the dog's age and that's that. Or for a vector use the sort algorithm: sort( dog.begin(), dog.end() ). You'll need to read up on sort().
Note that there is an STL/ATL/WTL message board which you should have used.
Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
|
|
|
|
|
Greg23234 wrote:
I have a vector full of objects... I wish to sort this vector by the return value of one of the object's methods.
You might want to have a look at the documentation for the std::sort that takes a predicate, where your predicate is something like
bool predicate(const dog& a, const dog& b)
{
return a.fn() < b.fn();
}
|
|
|
|
|
Okay I understand that in the regular C++, programs start at main() and run thru each line until it finishes. And that in MFC everything is controled by a message map. So If I want to run a function, without an event calling it like for a button, then I need to create a message map and call it.....right?
I mean why can't there be an easier way to call a function outside of events.
So my problem is that I've read the docs on messaging on CP and went thru the steps but every time I create a message map I get and error unresolved external symbol. Am I missing something?
My issue is this. I want to have me main dialog window come up. Then I want a function to run. My problem is that my function runs first then the main dialog window comes up. So how can I by pass this?
Is there an easier way of doing this?
Thanks
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
Tom Wright wrote:
So If I want to run a function, without an event calling it like for a button, then I need to create a message map and call it.....right?
No not right. If your function is not an event handler, then y do u need to use message maps for it? u can simply call it like any other function in C++.
Tom Wright wrote:
My problem is that my function runs first then the main dialog window comes up. So how can I by pass this?
To make sure that your dialog comes up first and your function is called next. Call your function in InitDialog() function after the call of base class InitDialog function call,instead of constructor for CDialog.
cheers
Muhammad Shoaib Khan
http://geocities.com/lansolution
|
|
|
|
|
Sorry I didn't mention that I am calling my function in the OnInitDialog() function, but my main windows still does not come up because it won't until after it hits the return TRUE.
Thanks anyway
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
Sorry I didn't mention that I am calling my function in the OnInitDialog() function, but my main windows still does not come up because it won't until after it hits the return TRUE except that it hits my function call first.
Thanks anyway
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
Where do you call this function? If you call it from OnInitDialog() then it will be executed before your dialog shows up. Post a message (WM_APP+number) at the end of OnInitDialog() to your app and do all your initialization in the event-handler of the WM_APP-message. This message will be received by your app directly after the dialog shows up.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Okay Mike Dunn told me the same thing.....but I have no idea what you are talking about. Can you give some more info on this?
For instance in my OnInitDialog() before the return TRUE, I put PostMessage(WM_APP+1);
After this you lost me. I have a clue as to what you are saying.
Thanks
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
Tom Wright wrote:
For instance in my OnInitDialog() before the return TRUE, I put PostMessage(WM_APP+1);
correct.
I haven't tested the next steps, but it should work:
Create a WindowProc for your CDialog-derived dialog with the class-wizard.
You could then check if the message is WM_APP+1. If it is, you simply call your function you want to execute. Example:
LRESULT CFavToHTMLDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_APP+1:
YourFunction();
break;
}
return CDialog::WindowProc(message, wParam, lParam);
}
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Can you guys recommend a good book that describes messaging in windows?
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
Programming Windows by Charles Petzold writen .I am studying now.
I am like programing!
|
|
|
|
|
Hello all,
Could someone please direct me to where I can get information (or better yet, sample code) on how to apply different time zones to system time (without changing the actual system time, of course).
I've looked at MSDN, but found nothing useful.
I also didn't find any way to obtain the list of time zones along with countries/cities that Windows itself uses for regional settings.
Any help greatly appreciated,
Thank you in advance,
MCP, MCSD
|
|
|
|
|
I had to do something similar at one time with software on an aircraft that could traverse multiple time zones, although limited to those in the US. Since the computer itself had only one time zone within which it lived, we always knew the time within its own zone. This can be found with a CTime object and the member function GetCurrentTime(). This CTime object can be formatted with the member function Format(), and the time zone can be parsed out, if needed.
Based on that known time zone, I made a drop-down list of US time zones by name that converted into hour offsets from the computer's zone. If they wanted local time to be referenced in Eastern Time, which they selected from the drop-down list, then this converted to adding 2 hours to the computer time before displaying.
It was fairly cheap and easy, but it did the job.
Good luck,
Dave
"You can say that again." -- Dept. of Redundancy Dept.
|
|
|
|
|
Yes, I understand that approach, but what I need is to go International
That's the problem - if I choose to implement it this way, then where can I get a list of all major countries/cities with their times zones?
Or is there another approach?
Anyway, thank you for replying,
MCP, MCSD
|
|
|
|
|
Sorry, without knowing any details, I couldn't give you EXACTLY what you wanted.
Is there any way you could let the users select an offset from GMT, rather than by selecting a city? If so, then you know your difference from GMT and could add their difference and come up with local time for them (?).
If not, I guess it would be nice to have a scrolling world map that they could select their area, similar to setting the clocks on the PC. I'm surprised there isn't one of those available as an ActiveX control.
Dave
"You can say that again." -- Dept. of Redundancy Dept.
|
|
|
|
|
David Chamberlain wrote:
Is there any way you could let the users select an offset from GMT, rather than by selecting a city?
That would be too easy...
Unfortunately, for most of my users, this will be too hard of a task - it's not a major part of my program, just a small nice addition I want to bring in. Besides, most people won't really know what time zone they're in, and even if they do know about their own time zone, they probably won't know about another zone of the world (for example to watch time in New York, Sydney and Kuwait).
David Chamberlain wrote:
scrolling world map that they could select their area
I've searched around, but didn't find such a control... That would be really nice though.
Thank you for replying,
MCP, MCSD
|
|
|
|
|
I am creating an application that will display flight simulator data that is
stored in memory.
(A reflective memory PCI card connected through a daisy chain to other
cards)
I have written the application that will read the data from memory, but now
I need to write a application that will only write specific values.
My boss wants me to create a header written to memory that is a blue print
of what values will be written.
This is what I am thinking:
typedef struct sim_address_data{
char *signal; //this is the name of the signal (ie pitch,yaw,roll)
????? *offset; //this is the offset from 0x0 to where the data will be written
//and I don't know what type this should be
double *value; //this will hold the value of the signal.
//all signals are doubles
double *min; //this will be the minimum that the value could be
double *max; //this will be the max that the value could
be
}rfm_sim_data;
rfm_sim_data sim_data[] = {//these are just examples
{"pitch", ??what ever offset??,functionCall_to_return_value_at(*offset), 0.0, 10.0};
{"yaw", ??what ever offset??, functionCall_to_return_value_at(*offset), -0.1, 5.0};
};
By writing the typedef to the first part of memory then any application can
access it to find out how the data is written.
and they will know how to parse the information in memory.
Do any of you experts think that this is the correct way to do this?
If not what would be a better way?
thanks,
Steven
|
|
|
|
|
Must .rc files be encoded as ASCII? Is there any way to have Visual Studio work with .rc files that are UTF-8 or UNICODE encoded?
How about .mc files? Can those be UTF-8/UNICODE encoded?
Jon Sagara
In India, when someone says "mad cow", you know it's actually a bull charging at him.
-- Rohit Sinha
|
|
|
|
|
Hi,
If I want to disable one of the menu options I have to write:
<br />
GetMenu()->EnableMenuItem(ID_FILE_OPEN,MF_BYCOMMAND|MF_ENABLED);<br />
But if I give a pointer of this menu to another window it does not work and even crash....
Why ?
|
|
|
|
|
I assume you are talking about the pointer returned from GetMenu()
right from MSDN :
The returned pointer may be temporary and should not be stored for later use.
So, you can't use it.
Maximilien Lincourt
For success one must aquire one's self
|
|
|
|
|
I am trying to write a C++ program to be used on Win2000 that talks through a RocketPort Serial Hub form Comport. The RocketPort connects to the computer via an ethernet connection and is addressed by virtual COM ports (currently COM3 - COM10). Basically, I will have a rack of devices with each device connected to one of the ports on the back of hub. Since the rack device use will be simple and controlled with function calls and development time is limited, I am using using non-overlapped I/O. From reading through the Platform SDK and Windows API documentation, non-overlapped I/O seems sufficient given how the devices will be used. The program is to write data to the devices in the rack and then receive and parse their response.
The basic structure of each of the program's functions is as follows:
1. Open the port using CreateFile(), and set control settings using
GetCommState() and SetCommState().
2. Write data to the rack device using WriteFile().
3. Receive data from the rack device using ReadFile() and parse the response.
4. Restore previous port settings and close the port.
The problem I am having is in using the Communication Time-outs. When I
don't use them, the program works fine. However, I need to use the
timeouts so that my program can report an error state if there is a
problem with communication (ex. one of the serial cables is unplugged). Without using the timeouts, the current program will
freeze on the ReadFile function if the computer is unable to communicate with the devices.
When I try to set the timeouts, using the COMMTIMEOUTS structure with
GetCommTimeouts() and SetCommTimeouts(), the WriteFile() function returns
as if the data was sent. However, the data isn't actually being sent.
Since the data is not actually being sent, the rack device doesn't respond and the program freezes waiting for the devices' response.
I determined the data is not being sent because (1) the port LED doesn't
flash, (2) the rack device doesn't respond, and (3) when I fill the
COMSTAT structure, using the ClearCommError() function after I called the
WriteFile() function, the cbOutQue member indicates that data is remaining to be transmitted even though the Microsoft Platform SDK explicitly states that this member is zero when non-overlapped operations are used.
Another interesting quirk, if I step through the program in debug mode,
the program actually works with the timeouts, but this is the only way it
will work when using the timeouts.
When I run this same program over the serial port on the back of the
computer (COM1), it works whether or not I use the timeouts. Its only when I try to use one of the ports on the RocketPort (COM3 - COM10), that I have this problem. Also, I've tried this by naming the ports both COM# and \\.\COM# (the naming convention that is suggested may be necessary in Comtrol's documentation) with both naming conventions producing the same results.
/**********************************************************************/
/* This function connects to and configures a COM port */
/**********************************************************************/
BOOL JM::OpenPort()
{
BOOL bSuccess;
dwLastError=ERROR_SUCCESS;
CString m_PortName = "COM3";
hFile = CreateFile(m_PortName,
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened w/exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if(hFile == INVALID_HANDLE_VALUE)
{
dwLastError = GetLastError();
return false;
}
bSuccess = GetCommTimeouts(hFile, &TimeOuts);
if (!bSuccess)
{
dwLastError = GetLastError();
return false;
}
old_TimeOuts = TimeOuts;
memset(&TimeOuts, 0, sizeof(COMMTIMEOUTS));
TimeOuts.ReadIntervalTimeout = 0;
TimeOuts.ReadTotalTimeoutMultiplier = 0;
TimeOuts.ReadTotalTimeoutConstant = 0;
TimeOuts.WriteTotalTimeoutMultiplier = 0;
TimeOuts.WriteTotalTimeoutConstant = 0;
bSuccess = SetCommTimeouts(hFile, &TimeOuts);
if (!bSuccess)
{
dwLastError = GetLastError();
return false;
}
bSuccess = GetCommState(hFile, &dcb);
if (!bSuccess)
{
dwLastError = GetLastError();
return false;
}
old_dcb = dcb;
dcb.BaudRate = CBR_9600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit
dcb.fBinary=true;
dcb.fDtrControl=DTR_CONTROL_ENABLE;
dcb.fRtsControl=RTS_CONTROL_ENABLE;
dcb.fParity=false;
dcb.fOutX=false;
dcb.fInX=false;
dcb.fNull=false;
dcb.fAbortOnError=false;
dcb.fOutxCtsFlow=false;
dcb.fOutxDsrFlow=false;
dcb.fTXContinueOnXoff=false;
dcb.fDsrSensitivity=false;
bSuccess = SetCommState(hFile, &dcb);
if (!bSuccess)
{
dwLastError = GetLastError();
return false;
}
return true;
}
/****************************************************************/
/* This is a boiler-plate query function that is called by each */
/* specific query function. *mstrRX is the CString where the */
/* ASCII response is stored. mstrCommand is the command string */
/* that determines what information is retrieved */
/****************************************************************/
BOOL JM::Query(CString *mstrRX, CString mstrCommand)
{
BOOL bWrite_chk, bRead_chk, bSuccess, bPort_chk;
CString TX_str, Rx_temp_str;
DWORD dwBytes_Written, dwBytes_Read;
char *pcRead_Buff = new char;
int i;
bPort_chk = OpenPort(); //Connect to port and configure
if(!bPort_chk)
return false;
TX_str += mstrCommand;
bWrite_chk = WriteFile(hFile,TX_str,TX_str.GetLength(),
&dwBytes_Written,NULL);
if(!bWrite_chk)
{
dwLastError = GetLastError();
return false;
}
bPort_chk = ClearCommError(hFile,&dwCommError,&PortStatus);
if(!bPort_chk)
return false;
i=0;
bRead_chk = ReadFile(hFile,pcRead_Buff,1,&dwBytes_Read,NULL);
if(bRead_chk)
{
while ((pcRead_Buff[i] != '\r'))
{
bRead_chk = ReadFile(hFile,pcRead_Buff,1,&dwBytes_Read,NULL);
Rx_temp_str += *pcRead_Buff;
}
*mstrRX = Rx_temp_str;
}
else
{
dwLastError = GetLastError();
return false;
}
bSuccess = ClosePort();
if(!bSuccess)
return false;
delete pcRead_Buff;
return true;
}
Thanks in advance for any help that can be offered.
|
|
|
|
|
This is how I normally configure the timeouts for a COM port:
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
timeouts.WriteTotalTimeoutMultiplier = 0;
SetCommTimeouts(m_hSerialPortHandle, &timeouts);
This means that the WriteFile() call doe snot return until the characters have been sent, but for ReadFile() this becomes a polling operation. I normally loop around a thread that tries to read a character every x ms and sleeps in between. This has always worked well for me. You just need to check the value of dwNumBytesRead in the ReadFile() call.
Hope its of help.
Roger Allen
Sonork 100.10016
Were you different as a kid? Did you ever say "Ooohhh, shiny red" even once? - Paul Watson 11-February-2003
|
|
|
|
|
I have a FormView based mfc program. I am trying to adjust my program so it can adapt to different screen resolution.
I have the following code in my OnDraw function, but it fails to adapt window size on different screen resolution.
CRect rc;
SystemParametersInfo(SPI_GETWORKAREA,0,rc,0);
I don't know what I'm missing, or if there is anything wrong with my code.
|
|
|
|
|
I would do it like this:
RECT rc;
SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
-Dominik
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
|
|
|
|
|