|
Thank You for your reply! It's GMT -07:00 here so I'll give your suggestion a try on the morrow.
Thanks again,
kerchunk
|
|
|
|
|
I made the changes you suggested but now the code does not compile. Your instructions were clear - did I fat-finger something?
[code]
// sort_example.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <list>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std;
struct pstchip_pin_row {
string primitive;
string pin_name;
string pin_number;
/* optional constructors */
pstchip_pin_row( string aa, string bb, string cc) {
primitive = aa;
pin_name = bb;
pin_number = cc;
}
//overridding operators
pstchip_pin_row& operator = (const pstchip_pin_row &itemToCopy) {
this->primitive = itemToCopy.primitive;
this->pin_name = itemToCopy.pin_name;
this->pin_number = itemToCopy.pin_number;
return *this;
}
bool operator == ( const pstchip_pin_row& itemToCompare) {
if( this->primitive == itemToCompare.primitive && this->pin_name < itemToCompare.pin_name ) return true;
return false;
}
bool operator < ( const pstchip_pin_row& itemToCompare) {
if( this->primitive < itemToCompare.primitive ) return true;
if( this->primitive == itemToCompare.primitive && this->pin_name < itemToCompare.pin_name) return true;
return false;
}
};
struct pstchip_pin_wor {
string primitive;
string pin_name;
string pin_number;
/* optional constructors */
pstchip_pin_wor( string aa, string bb, string cc) {
primitive = aa;
pin_name = bb;
pin_number = cc;
}
//overridding operators
pstchip_pin_wor& operator = (const pstchip_pin_wor &itemToCopy) {
this->primitive = itemToCopy.primitive;
this->pin_name = itemToCopy.pin_name;
this->pin_number = itemToCopy.pin_number;
return *this;
}
bool operator == ( const pstchip_pin_wor& itemToCompare) {
if( this->primitive == itemToCompare.primitive && this->pin_name < itemToCompare.pin_name ) return true;
return false;
}
bool operator < ( const pstchip_pin_wor& itemToCompare) {
if( this->primitive < itemToCompare.primitive) return true;
if( this->primitive == itemToCompare.primitive && this->pin_name < itemToCompare.pin_name) return true;
return false;
}
// this function really just wraps pstchip_pin_wor::operator< so that it can accept pointers
static bool compareFunc( const pstchip_pin_wor* a, const pstchip_pin_wor* b) {
return *a < *b;
}
};
//main function
int main(int argc, char* argv[]) {
int nRetCode = 0;
list<pstchip_pin_row> rows_pstchip_pins;
list<pstchip_pin_wor*> wors_pstchip_pins;
//populate list
rows_pstchip_pins.push_back( pstchip_pin_row( "Europe", "Spain", "Madrid" ) );
rows_pstchip_pins.push_back( pstchip_pin_row( "North America", "USA", "Washington DC" ) );
rows_pstchip_pins.push_back( pstchip_pin_row( "North America", "Canada", "Ottawa" ) );
rows_pstchip_pins.push_back( pstchip_pin_row( "Europe", "Germany", "Berlin" ) );
rows_pstchip_pins.push_back( pstchip_pin_row( "South America", "Brazil", "Sao Paulo" ) );
rows_pstchip_pins.push_back( pstchip_pin_row( "North America", "Mexico", "Mexico City" ) );
wors_pstchip_pins.push_back( new pstchip_pin_wor( "Europe", "Spain", "Madrid" ) );
wors_pstchip_pins.push_back( new pstchip_pin_wor( "North America", "USA", "Washington DC" ) );
wors_pstchip_pins.push_back( new pstchip_pin_wor( "North America", "Canada", "Ottawa" ) );
wors_pstchip_pins.push_back( new pstchip_pin_wor( "Europe", "Germany", "Berlin" ) );
wors_pstchip_pins.push_back( new pstchip_pin_wor( "South America", "Brazil", "Sao Paulo" ) );
wors_pstchip_pins.push_back( new pstchip_pin_wor( "North America", "Mexico", "Mexico City" ) );
cout << "before sorting in list" << endl << endl;
for( list<pstchip_pin_row>::iterator gh = rows_pstchip_pins.begin() ; gh != rows_pstchip_pins.end() ; gh++ ) {
cout << gh->primitive << "==" << gh->pin_name << "==" << gh->pin_number << endl;
}
rows_pstchip_pins.sort();
cout << endl << "after sorting" << endl << endl;
for( list<pstchip_pin_row>::iterator uy = rows_pstchip_pins.begin() ; uy != rows_pstchip_pins.end() ; uy++ ) {
cout << uy->primitive << "==" << uy->pin_name << "==" << uy->pin_number << endl;
}
cout << endl << endl << "before sorting using pointers in list" << endl << endl;
for( list<pstchip_pin_wor*>::iterator ka = wors_pstchip_pins.begin() ; ka != wors_pstchip_pins.end() ; ka++ ) {
cout << (*ka)->primitive << "==" << (*ka)->pin_name << "==" << (*ka)->pin_number << endl;
}
wors_pstchip_pins.sort( pstchip_pin_wor::compareFunc);
cout << endl << "after sorting" << endl << endl;
for( list<pstchip_pin_wor*>::iterator zm = wors_pstchip_pins.begin() ; zm != wors_pstchip_pins.end() ; zm++ ) {
cout << (*zm)->primitive << "==" << (*zm)->pin_name << "==" << (*zm)->pin_number << endl;
}
return nRetCode;
} //end-function
[/code]
TIA,
kerchunk
|
|
|
|
|
You fattened the parameters of compareFunc with const specifiers. It works when you remove them.
If you want to define compareFunc with const parameters:
static bool compareFunc( const pstchip_pin_wor* a, const pstchip_pin_wor* b)
then you have to define the operator < as a const member:
bool operator < ( const pstchip_pin_wor& itemToCompare) const
modified on Wednesday, December 2, 2009 5:13 PM
|
|
|
|
|
OK I unfattened compareFunc by removing the two 'const's.
Under VC++6 the program now throws one compiler error:
---------
C:\mystuff\sort_example.cpp(127) : error C2664: 'void __thiscall std::list<struct pstchip_pin_wor *,class std::allocator<struct pstchip_pin_wor *> >::sort(struct std::greater<struct pstchip_pin_wor *>
)' : cannot convert parameter 1 from 'bool (__cdecl *)(struct pstchip_pin_wor *,struct pstchip_pin_wor *)' to 'struct std::greater<struct pstchip_pin_wor *>'
No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing cl.exe.
----------
??? Do I need to overload the std::greater function? And if so how?
Thank You,
kerchunk
|
|
|
|
|
BUG: The STL list::sort() Function Doesn't Sort a List of Pointers
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q265109
The Standard Template Library (STL) list::sort function doesn't sort a list of pointers when a predicate function is defined for sorting.
VC6 is a ten-year old compiler now. The bug is not present in VC2008 or MinGW. Your code is implemented below with the workaround described in KB265109.
________________________________________________________________________________________
#include <iostream>
#include <string>
#include <list>
using namespace std;
struct pstchip_pin_wor
{
string primitive;
string pin_name;
string pin_number;
pstchip_pin_wor( string aa, string bb, string cc)
{
primitive = aa;
pin_name = bb;
pin_number = cc;
}
pstchip_pin_wor& pstchip_pin_wor::operator= (const pstchip_pin_wor &itemToCopy)
{
if(&itemToCopy != this)
{
this->primitive = itemToCopy.primitive;
this->pin_name = itemToCopy.pin_name;
this->pin_number = itemToCopy.pin_number;
}
return *this;
}
int pstchip_pin_wor::operator == ( const pstchip_pin_wor& itemToCompare) const
{
if( this->primitive == itemToCompare.primitive && this->pin_name < itemToCompare.pin_name )
return 1;
return 0;
}
int pstchip_pin_wor::operator < ( const pstchip_pin_wor& itemToCompare) const
{
return Compare(this, &itemToCompare);
}
static bool Compare(const pstchip_pin_wor* elem1, const pstchip_pin_wor* elem2)
{
if( elem1->primitive < elem2->primitive )
return 1;
if( elem1->primitive == elem2->primitive && elem1->pin_name < elem2->pin_name)
return 1;
return 0;
}
};
/* ADDED ACCORDING TO KB265109 */
template<>
struct std::greater<pstchip_pin_wor*> : public binary_function<pstchip_pin_wor* ,pstchip_pin_wor*, bool>
{
bool operator()(const pstchip_pin_wor* &elem1, const pstchip_pin_wor* &elem2) const
{
return pstchip_pin_wor::Compare(elem1, elem2);
};
};
/* ADDED ACCORDING TO KB265109 */
struct SortFunc
/* ADDED ACCORDING TO KB265109 */
: public greater<pstchip_pin_wor*>
/* ADDED ACCORDING TO KB265109 */
{
bool operator()(pstchip_pin_wor*& elem1, pstchip_pin_wor*& elem2)
{
return pstchip_pin_wor::Compare(elem1, elem2);
}
};
int main(int argc, char* argv[])
{
int nRetCode = 0;
list<pstchip_pin_wor*> wors_pstchip_pins;
wors_pstchip_pins.push_back( new pstchip_pin_wor( "Europe", "Spain", "Madrid" ) );
wors_pstchip_pins.push_back( new pstchip_pin_wor( "North America", "USA", "Washington DC" ) );
wors_pstchip_pins.push_back( new pstchip_pin_wor( "North America", "Canada", "Ottawa" ) );
wors_pstchip_pins.push_back( new pstchip_pin_wor( "Europe", "Germany", "Berlin" ) );
wors_pstchip_pins.push_back( new pstchip_pin_wor( "South America", "Brazil", "Sao Paulo" ) );
wors_pstchip_pins.push_back( new pstchip_pin_wor( "North America", "Mexico", "Mexico City" ) );
cout << "before sorting" << endl << endl;
for( list<pstchip_pin_wor*>::iterator ka = wors_pstchip_pins.begin(); ka != wors_pstchip_pins.end(); ka++ )
{cout << (*ka)->primitive << "==" << (*ka)->pin_name << "==" << (*ka)->pin_number << endl;}
wors_pstchip_pins.sort(SortFunc());
cout << endl << "after sorting" << endl << endl;
for( list<pstchip_pin_wor*>::iterator zm = wors_pstchip_pins.begin(); zm != wors_pstchip_pins.end(); zm++ )
{cout << (*zm)->primitive << "==" << (*zm)->pin_name << "==" << (*zm)->pin_number << endl;}
return nRetCode;
}
|
|
|
|
|
WOOHOO!! It works great! Good catch, I'd've never thought to check the compiler itself, and lo sure enough there's a KB against exactly this issue.
Thank You So Much for seeing me through this!
CASE CLOSED
kerchunk
|
|
|
|
|
How to get it without HWND?
If activex windowed i use GetRandomRgn(GDI) function,
and i want to get clipped region for windowless control the same as GetRandomRgn do it for windowed.
How to do it?
|
|
|
|
|
Hi ,
I am using transparent image in LoadBitmap image. When i compile code , then compiler shows error as
"The bitmap must be redrawn or converted to 3.x format" Anybody know how to convert bmp to 3.x format.
If I want to use GIf image in ATL COM to display it in any window then how can i use GIF image into window.
Regards
am
|
|
|
|
|
May be you can use ATL CImage class to display the image.
|
|
|
|
|
I want to render IWMPPlayer to play stream media online in my demo project. When i create such a com instance ant call put_URL, it start playing the media as i want successfully. But, the problem is that, i can't stop it except i close the application. Here is how i stop the playing:
IWMPControls* pICtrl;
m_pIPlayer->get_controls(&pICtrl);
pICtrl->stop();//it returns S_FALSE;
And, another problem is that, when i call put_URL to redirect the url to another server, there will be two active radio channel playing in the same time!
Why?
|
|
|
|
|
I have to embed an ActiveX control into an simple WIN32 app. The parent window is created with CreateWindow() and the control - a MS Flex Grid Control - is embedded with the following code (ATL):
#import "../msflxgrd.ocx"
using namespace MSFlexGridLib;
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
....
_Module.Init(NULL, hInstance);
AtlAxWinInit();
...
}
atlWndClassName = CAxWindow2::GetWndClassName();
axctrl = ::CreateWindow(atlWndClassName,
_T("{6262d3a0-531b-11cf-91f6-c2863c385e30}"),
WS_CHILD, 310, 10, 300, 300,
(HWND)hWndParent, (HMENU)10, hInst, NULL);
ShowWindow(axctrl, SW_SHOW);
UpdateWindow(axctrl);
Everything works fine with this example, but it is not possible to move from cell to cell with the arrow keys. When i connect Spy++ to the application, i can see, that WM_KEYDOWN and WM_KEYUP messages are sent to the Flex Grid Control. The same problem exists with the MS Calendar Control and other controls.
When these controls are embedded into the Microsoft ActiveX Control Test Container or into a MFC-Dialogbox the keyboard control works fine.
Has someone an idea, what code is necessary to support keyboard control for ActiveX controls in simple WIN32 apps?
|
|
|
|
|
Hello, folks,
I want a menu like WinXP start menu. If it has to many programs installed. the menu will split into more than one column.
Thanks
Hawk
|
|
|
|
|
I got it. use MFT_MENUBREAK or MFT_OWNERDRAW.
|
|
|
|
|
Hello together,
may be this is a simple question. But I am missing a piece to put it all together. I have searched for patterns how to use COM in real life, but nothing told me that. I know how to implement interfaces, properties and methods and so on.
So here is what I want to do.
I have a normal C++ object from a class like CSomeObject. This class implements a bunch of methods. Now I want to use this methods not only in the program itself but also in the outside world. So I put all the methods I want to use in a normal interface.
class CSomeObject : public ICommandProvider
{
...
};
class ICommandProvider
{
public:
void Command1();
void Command2(int arg);
};
Then I want to create a CoClass offering an interface like ICommandProvider, let's name the COM interface ICommands.
interface ICommands : IDispatch
{
HRESULT Command1();
HRESULT Command2([in] int arg);
};
And the COM object is implemented using the ATL framework. The COM object should have a private pointer to the normal ICommandProvider interface.
class ATL_NO_VTABLE CoCommands :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CoCommands, &CLSID_CoCommands>,
public IDispatchImpl<ICommands, &IID_ICommands, &LIBID_MyLib>
{
public:
CoCommands();
...
private:
ICommandProvider *m_pCommandProvider;
};
Now it comes to my problem. I don't know how to set up the pointer.
I can't do it in the constructor, because one need a default constructor to create a COM object using the CreateInstance methods.
May be a solution is to implement another COM interface to set up the private pointer. But this sounds like overhead I don't need, right?
So what is the normal pattern to set up a COM object with the internal data it should offer to some client?
Best regards!
|
|
|
|
|
In my experience COM is all about performing separate tasks like initalisation on different interfaces. You can either implement another interface to set your object up or add an Init(...) method to your existing interface which does the job but mixes up the two tasks. Various IPersist..... interfaces have InitNew or Load methods that might suit your needs so you don't have to reinvent the wheel.
|
|
|
|
|
Hello mbet878,
If the source codes for the CSomeObject class is in the same project as CoCommands (your ATL class), then there is nothing to stop you from doing something like the following :
m_pCommandProvider = new CSomeObject();
either in the constructor for CoCommands or in the FinalConstruct() function.
Then in the destructor for CoCommands or in the FinalRelease() function, call :
delete m_pCommandProvider;
m_pCommandProvider = NULL;
Have you tried the above ?
- Bio.
|
|
|
|
|
At the end the solution was very simple. I added just a normal Set method to the CoClass itself. Then I created the COM object using the CComObject<> template and calling the CreateInstance method. After this I just called the Set method on the COM object. After this was done I asked for the interface I want to have and handed it out.
Thanks for the replies!
MBet
|
|
|
|
|
My aim is to learn the data structure, in the QuickSort algorithm . 0.6s to complete the order that is 1 million random numbers.
1. However, if the rand number such as rand ()% 2, such a random number will be occur stack overflow , the problem is very difficult for me , in order to keeping the rapid nature of QuickSort, may I ask how to solve the stack overflow problem.
2. But luckily no stack overflow if the situation is still rand ()% 2, such a random number calculate the time to reach 17s.
Thank you very much.
Attached on the source code:
void SwitchNum(int& n1,int& n2)
{
int nTemp=n1;
n1=n2;
n2=nTemp;
}
void QuickSort(int *pnArray,int nSize)
{
if (nSize>1)
{
int nNum=nSize/2,i=0,j=nSize-1;
while (i!=j)
{
for (;j>nNum;j--)
{
if (pnArray[nNum]>pnArray[j])
{
SwitchNum(pnArray[nNum],pnArray[j]);
nNum=j;
break;
}
}
for (;i <nNum;i++)
{
if (pnArray[nNum] <pnArray[i])
{
SwitchNum(pnArray[nNum],pnArray[i]);
nNum=i;
break;
}
}
}
QuickSort(pnArray,nNum);
QuickSort(pnArray+nNum+1,nSize-nNum-1);
}
}
|
|
|
|
|
crazy66 wrote: may I ask how to solve the stack overflow problem.
QuickSort() is a recursive procedure, so I can only guess that your recursion goes to too deep a level, hence stack overflow.
|
|
|
|
|
Im a beginner of WTL. I want to subclass a CStatic control, my code is very simple like following:
class CMyStatic : public CWindowImpl(CMyStatic,CStatic)
{
BEGIN_MSG_MAP_EX( CMyStatic)
//message handle macros here
END_MSG_MAP( )
//there are message implements
}
And, in the main dialog window, I use SubclassWindow function to subclass a spcified static control. But, the problem iss, when the main dialog is closing, i would get an atl assert:
ERROR - Object deleted before window was destroyed.
I know the problem is due to the deconstructor of its base class. It's deleted before the window has been destroyed. So, how to fix it?
|
|
|
|
|
Unsubclass[^] the window in a WM_CLOSE handler?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
You means Unsubclass the child control in its parent's WM_CLOSE message handler? Yes, i calls myContrl.UnSubclassWindow() before EndDialog() call.
|
|
|
|
|
Im sorry, its my fault, i had declared two such controls, but unsubclssed only one. its ok now.
Regards.
|
|
|
|
|
Hi,
I created an ATL Exe Service using classwizard (VS60) and it contains 1 method. I installed the service using the -service keyword and manually started the service. However, when my client application tries to call the method of that service it basically hangs on the call. Any ideas?
Thanks
|
|
|
|
|
|