Cry about...
MS-Windows Troubleshooting


error C2258: illegal pure syntax, must be '= 0'


These notes have kindly been provided by Sujith Joseph, in India:

Symptom:

Problem: Error C2258 and error C2252

I declared an ATL COM class CPolyGlotOPCServerObject in the file CPolyGlotOPCServerObject.h and implemented it in CPolyGlotOPCServerObject.h. I declared a member variable(STL wstring) to the class and initialised it to a string as follows:

wstring wstringOPCServerVendorName = L"HInfinity Corporation.PolyGlot OPCServer.Version 1.0";

and compiled to get the following errors (MS Visual C++6, Service Pack 6):

CPolyGlotOPCServerObject.cpp
c:\documents and settings\sujith joseph\my documents\work\polyglotopcserver\cpolyglotopcserverobject.h(52) : error C2258: illegal pure syntax, must be '= 0'
c:\documents and settings\sujith joseph\my documents\work\polyglotopcserver\cpolyglotopcserverobject.h(52) : error C2252: 'wstringOPCServerVendorName' : pure specifier can only be specified for functions
CUniqueStringCreator.cpp
opccomn_i.c
opcda_i.c
PolyGlotOPCServer.cpp
c:\documents and settings\sujith joseph\my documents\work\polyglotopcserver\cpolyglotopcserverobject.h(52) : error C2258: illegal pure syntax, must be '= 0'
c:\documents and settings\sujith joseph\my documents\work\polyglotopcserver\cpolyglotopcserverobject.h(52) : error C2252: 'wstringOPCServerVendorName' : pure specifier can only be specified for functions
Generating Code...
Error executing cl.exe.

PolyGlotOPCServer.dll - 4 error(s), 0 warning(s)

Solution

I tried to initialise a the member variable on declaration. C++ does not allow this. Initialisation of the member variable should be done in constructor as shown in code below. This error may be difficult to spot because it is almost natural for programmers to initialise a variable on its declaration. Unless you remember the C++ language rule above a lot of time can be wasted on finding this error.

/*=================================================================
Date : August 06, 2004 13:00
Last Modified : August 06, 2004 13:00
Class : CPolyGlotOPCServerObject
Filename : CPolyGlotOPCServerObject.h
Author : Sujith Joseph,
Email : sujith_joseph@hotmail.com
Copyright : Sujith Joseph
Information : Definition file for the class CPolyGlotOPCServerObject


==================================================================*/
#ifndef __CPOLYGLOTOPCSERVEROBJECT_H_
#define __CPOLYGLOTOPCSERVEROBJECT_H_


#include <atlbase.h>
#include <objbase.h> //The STDMETHOD macros are defined in this file
#include <vector> //STL vector container class requires this header file
#include <string> //STL wide character string class requires this header file
#include "resource.h" //Main symbols
#include "opccomn_i.c" //UUIDs for OPCFoundation required interface IOPCServer
#include "opccomn.h" //C++ declarations of IOPCCommon interface
#include "opcda_i.c" //UUIDs for OPCFoundation required interface IOPCServer
#include "COPCGroupObject.h" //Header file for the OPCGroupObject class COPCGroupObject
#include "opcda.h" //C++ declarations of IOPCServer interface

using namespace std; // Hoist the standard namespace for using the Standard Template Library


//Class Declaration for class CPolyGlotOPCServerObject
class ATL_NO_VTABLE
CPolyGlotOPCServerObject:

//Begin: Base classes for CPolyGlotOPCServerObject
public CComObjectRootEx<CComMultiThreadModel>,
public CComCoClass<CPolyGlotOPCServerObject, &CLSID_CPolyGlotOPCServerObject>,
public ISupportErrorInfo, public IConnectionPointContainerImpl<CPolyGlotOPCServerObject>, //A COM class implementing ISupportErrorInfo indicates that it can create a COM error object and return error info to the client
public ICPolyGlotOPCServerObject, // Suj: Default dummy interface to fool ATL Object Wizard. Delete at the end.
public IOPCServer, // Required OPCFouncation interface
public IOPCCommon, // Required OPCFouncation interface
public IOPCItemProperties, // Required OPCFoundation interface
public IOPCBrowseServerAddressSpace // Optional OPCFoundation interface. Although optional this is a defacto required interface
//End: Base classes for CPolyGlotOPCServerObject
{


//Member data variables

wstring wstringOPCServerVendorName = L"HInfinity Corporation.PolyGlot OPCServer.Version 1.0"; //Error C2258 and error C2252. C++ does not allow initialisation of class member variable on declaration


/*Once an OPC client has started running, it will create one or more OPCGroupObjects. Below
an STL container(vector) of OPCGroupObjects is declared. As each new OPCGroupObject is
created on client request, it will be put into the last element of the STL container
class and initialised.*/

public:

std::vector<COPCGroupObject> vectorCOPCGroupObjects; //STL container (vector) of OPCGroupObject classes

public:
CPolyGlotOPCServerObject(void)
{
wstringOPCServerVendorName = L"HInfinity Corporation.PolyGlot OPCServer.Version 1.0"; //Solution to Error C2258 and error C2252. Initialisation of class member variables must be done in constructor


} //Constructor

/*================================================================
ATL Macros for this class
==================================================================*/
DECLARE_REGISTRY_RESOURCEID (IDR_CPOLYGLOTOPCSERVEROBJECT)
DECLARE_PROTECT_FINAL_CONSTRUCT ()


/*=================================================================
The COM Interface map for this class
==================================================================*/
BEGIN_COM_MAP (CPolyGlotOPCServerObject)
COM_INTERFACE_ENTRY (ICPolyGlotOPCServerObject)
COM_INTERFACE_ENTRY (ISupportErrorInfo)
COM_INTERFACE_ENTRY (IConnectionPointContainer)
COM_INTERFACE_ENTRY (IOPCCommon) /* Interface map entry for the required OPCFoundation interface IOPCCommon */
COM_INTERFACE_ENTRY (IOPCServer) /* Interface map entry for the required OPCFoundation interface IOPCServer */
COM_INTERFACE_ENTRY (IOPCItemProperties) /* Interface map entry for the required OPCFoundation interface IOPCItemProperties */
COM_INTERFACE_ENTRY (IOPCBrowseServerAddressSpace) /* Interface map entry for the optional OPCFoundation interface IOPCBrowseServerAddressSpace*/
END_COM_MAP ()


/*===============================================================
The COM ConnectionPoint Map for this class
=================================================================*/
BEGIN_CONNECTION_POINT_MAP (CPolyGlotOPCServerObject)
END_CONNECTION_POINT_MAP ()


/*==============================================================
Methods of the ISupportsErrorInfo interface
================================================================*/
STDMETHOD (InterfaceSupportsErrorInfo)(REFIID riid);


};//End: Class definition


#endif // Include guard: CPOLYGLOTOPCSERVEROBJECT_H_



About the author: is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.