Click to See Complete Forum and Search --> : Class Constructor problem
Will Rothwell
April 27th, 1999, 10:44 AM
Hi all,
I used the class wizard to create a new class. The class I need is a sort of wrapper class for another class I use. I cannot find the right base class to derive from so I pick generic CWnd. The problem is I need to have a parameterised constructor like CHelper::CHelper(int nMode) so I can initialised some variables.
I cannot compile for something like duplicate default constructor exists errors. Class wizard gave me CHelper::CHelper() but I need more. Can any one tell me how to it. I am sure we can have more than one constructor in a class. I believe something to do with the MESSAGE MAP but I am not sure. Any suggestion please.
Will
LALeonard
April 27th, 1999, 10:52 AM
Please post the exact error message you get.
LA Leonard - http://www.DefinitiveSolutions.com
Will Rothwell
April 27th, 1999, 11:11 AM
Hi,
The exact error message is as follows: One of my required helper classes is a wrapper for some PKI api
C:\DDK\src\ThSS\APP\Scr\PKI.h(69) : warning C4520: 'CPKI' : multiple default constructors specified
When I create an object with the default constructor, i.e. no parameter, like CPKI pki; then I got compilation error of:
C:\DDK\src\ThSS\APP\SCR\AddUserEnvelope.cpp(461) : error C2668: 'CPKI::CPKI' : ambiguous call to overloaded function
The wrapper class don't really need to receive message, so could I kill the message thingie.
Thanks,
Will
sally
April 27th, 1999, 08:17 PM
1) can we have a look at your class definition as well?
2) don't remove the message thingie yet
sally
Sally
April 27th, 1999, 08:17 PM
1) can we have a look at your class definition as well?
2) don't remove the message thingie yet
sally
Will Rothwell
April 28th, 1999, 01:13 AM
Thanks for your response,
I put the definition here and I hope someone may spot the 'mistake' I introduced.
#ifndef _PKI
#define _PKI
#if !defined(AFX_PKI_H__09270250_F267_11D2_B8D1_0000B450801F__INCLUDED_)
#define AFX_PKI_H__09270250_F267_11D2_B8D1_0000B450801F__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// PKI.h : header file
//
#define PKI_RETURN_ALL 100
#define PKI_WITH_VALID_ENC_CERT_ONLY 101
#define PKI_WITH_VALID_SIGN_CERT_ONLY 102
/////////////////////////////////////////////////////////////////////////////
// CPKI command target
class CPKI : public CWnd
{
public:
CPKI();
//CPKI(int nToolKitsID = ENTRUST); /** This does not work **/
~CPKI();
// Attributes
public:
// Operations
public:
int GetToolKitID();
void SetToolKits(int nToolKitID);
//
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CPKI)
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CPKI)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#endif
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_PKI_H__09270250_F267_11D2_B8D1_0000B450801F__INCLUDED_)
Will
Rudolf
April 28th, 1999, 02:37 AM
//CPKI(int nToolKitsID = ENTRUST); /** This does not work **/
I think, the problem is the part "=ENTRUST".
How can the compiler tell whether a call
CPKI() means the default constructor or Your constructor with the parameter "ENTRUST"?
With CPKI(int nToolKitsID) it should work.
HTH
Rudolf
Jason Teagle
April 28th, 1999, 02:42 AM
It is the default value of the parameter to the second constructor that is causing the problem. Tackling the second error message first, think about it: if you call
---
CPKI *pPKI ;
pPKI = new CPKI(); // No parameter explicitly supplied.
---
how does the compiler know whether you are calling the CPKI(void) constructor or the CPKI(int nToolKitsID) version with the default parameter? A call to the constructor to the class with this definition can legally take no parameters for either version of the constructor, and so doesn't know which it is meant to use.
The ONLY way to have multiple (overloaded) methods of ANY kind, not just constructors, is if they differ by at least one more or less parameter, or a parameter of different types; if you specify a default value for a parameter, you are effectively saying that the parameter is not needed. Therefore, CPKI(void) and CPKI(nToolKitsID = ENTRUST) look the same - hence the first error message about two default constructors.
You have three choices to cure this - (1) do not make the parameter have a default value. After all, how difficult is it to always supply a parameter? (2) Add a second, dummy (and non-defaulted!) parameter to the second constructor so that even ignoring the defaulted parameter it looks different from the original. You do not actually have to use this other parameter at all. (3) Just use the original blank constructor and explicitly call SetToolKits(). This method can then have a default value.
I recommend option (1).
Hope this helps you to understand what is wrong.
Will Rothwell
April 28th, 1999, 03:54 AM
A big thank you to all your good people out there pointing out this silly mistake.
Will
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.