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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Varable Parameter (...)

Status
Not open for further replies.

micc2

Programmer
Dec 5, 2003
8
GB
I have created a class (in a DLL) with a constructor using a variable number of parameters :-

CUnitGrid(int numParams, ...)

When I try to declare a new test object (outwith the DLL) I get error messages including "syntax error : 'constant' " :-

CUnitGrid m_Grid(1,1);

A colleague said this was because the declaration is in a Header file. To try to work around this, I tried to declare the overloaded constructor inline. However, that did not work, I got the same error.

How can I get this code to work?

BACKGROUND
I am working with existing code with a large number of grids.
Many of these grids contain columns of data displaying metric values.
My task is to handle automatic conversion from Metric to Imperial when a menu option is selected.
I need to be able to identify which columns in the grid are displaying Metric values so that I can convert them. I was hoping to use the variable parameters list to say "this grid has unit values in columns 3,6 and 9".

 
I can't reproduce your case (VC 6.0). Variable parameters constructor works fine (but it's a rather dangerous style;). No difference where declaration placed - in-place or in a header file.
It seems, need more information about programming (not specification) context...
 
ArkM,
Thanks for your help.
Here is the code context - the call is in a class declaration in a header file, and it produces the following error:-

ERROR MESSAGE
c:\source\code\moddev\src\genview\unitsdialog.h(39) : error C2059: syntax error : 'constant'

CORRESPONDING LINE (39) in class declaration :-
class CUnitsDialog : public CDialog
{
// Construction
public:
CUnitsDialog(CWnd* pParent = NULL); // standard constructor

// Dialog Data
//{{AFX_DATA(CUnitsDialog)
enum { IDD = IDD_UNIT_CONVERSION };
CListBox c_ListBox;
CString m_ListSelection;
float m_dTemperature;
double m_dFeetInches;
// CMSHFlexGrid m_Grid;
//}}AFX_DATA

(39)--->>> CUnitGrid m_Grid(1,1);
...

Additional information :-
the Grid is declared as a component in a dialog. This is crucial to my plans, as I want to localize the changes as much as possible. If I can get away with only changing the declaration, it will help me a lot.

N.B. I specify the columns at CODING TIME.

EXAMPLE: The existing code contains a grid with 10 cols, 3 of which (cols 3,6, and 9) contain units data to be converted.
Existing code declaration (within another class declaration) :-
CFmGrid m_ComponentsGrid;

Proposed new declaration in same place :-
CUnitsGrid m_ComponentsGrid(3,3,6,9);


 
Sorry, but can you post CUnitGrid class constructor declaration (fragment)? Additionally, C2059 err msg may have the source on a line above...
Can you reproduce the error on a code sceleton, with only CUnitGrid-like refined class declaration and CUnitGrid x(1,1) definition?
What VC++ version etc?
 
ArkM,
I have been trying an alternative, which shows that the variable parameter list is a red herring.

I have changed the overloaded constructor to use a simple unsigned int parameter (which I intend to fill using a function with variable parameters, but that is not significant in this wee test).

NEW CONSTRUCTOR DECLARATION=====================
This code is in a Header file in a DLL :-

class AFX_EXT_CLASS CUnitGrid : public CFmGrid
{
public:
CUnitGrid();
CUnitGrid(unsigned int);
===================================================

When I try to use the new constructor I get the same error message error C2059: syntax error : 'constant':-

NEW ATTEMPTED USE OF OVERLOADED CONSTRUCTOR=============
This code is a Header file outside the DLL, and the constructor call is inside a dialog box class declaration :-

CUnitGrid m_Grid(0);
==========================================================
 
OK, I've managed to find out what is happening.
I cannot call the overloaded constructor from the dialog's class definition.

The existing code typically has a dialog box whose class contains several components including Grids.

The Grid is declared inside the dialog class, and when the dialog is created, the default constructor for the Grid is called.

If I want to derive a new UnitGrid from the existing grid, I need to change the declaration from

CFmGrid m_Grid;
to
CUnitGrid m_Grid;


However, I CANNOT specify the columns requiring metric/imperial conversion here, because that would mean calling an overloaded constructor thereby attempting to create an object without a parent.

The way to create a member object is in the member initialisation list of the parent (dialog) constructor as follows :-

CUnitsDialog::CUnitsDialog(CWnd* pParent /*=NULL*/)
: CDialog(CUnitsDialog::IDD, pParent),
m_Grid(3,3,6,9)
{...}

This is unfortunate, as it means 2 places the code has to be changed instead of 1, but if it has to be done then so be it.

Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top