Re: Error - Declaring static arrays of structs in a class (VC6)

From: benben (benhongh_at_yahoo.com.au)
Date: 12/24/04


Date: Fri, 24 Dec 2004 10:40:02 +0800

But I think it is also a good style to supply a constructor, and init the
struct as

S(1, "Hi")

rather than

{1, "Hi"}

VC 7.1 is sometimes too forgiving to not-so-good style code. :)

benben

> VC6 didn't seem to allow initializing non-POD structures with an
initializer
> list.
>
> "Hashim" <hashim@nowhere.com> wrote in message
> news:e9b8oEN6EHA.2196@TK2MSFTNGP14.phx.gbl...
> > Yes, I came to know from another forum that this code compiles fine on
VC7
> > but they didnt tell to change the code
> >
> > {5, "Hi"}
> >
> > to
> >
> > {5, string("Hi")}
> >
> > Thanks. This seems logical and understandable.
> >
> > I am still waiting for the explanation of the solution presented in
first
> > reply of my post.
> >
> >
> > "benben" <benhongh@yahoo.com.au> wrote in message
> > news:OAMOueM6EHA.3756@TK2MSFTNGP14.phx.gbl...
> >> Your code compiled fine on my machine (VC++ 7.1). Perhaps if you change
> >>
> >> CMyClass::S CMyClass::Array[] =
> >> {
> >> {5, "Hi"}
> >> };
> >>
> >> to
> >>
> >> CMyClass::S CMyClass::Array[] =
> >> {
> >> {5, string("Hi")}
> >> };
> >>
> >> will lead you out of the hassal.
> >>
> >> On a 32-bit intel machine {5, "Hi"} will take up 64 bits (32 bits for
an
> >> int, 32 bits for a const char*), however, sizeof(CMyClass::S) may take
up
> >> bore bits (32 bits for an int, more than 32 bits for an std::string)...
> >>
> >> ben
> >>
> >>> Hi all,
> >>>
> >>> This is the .h file
> >>>
> >>>
> >>> Code:
> >>> #include <string>
> >>> using namespace std;
> >>>
> >>> class CMyClass
> >>> {
> >>> private:
> >>> struct S
> >>> {
> >>> int i;
> >>> string s;
> >>> };
> >>>
> >>> static S Array[];
> >>> static int i[];
> >>>
> >>> public:
> >>> CMyClass();
> >>> virtual ~CMyClass();
> >>>
> >>> };
> >>>
> >>> and this the .cpp file
> >>>
> >>>
> >>> Code:
> >>> #include "MyClass.h"
> >>>
> >>>
> >>> CMyClass::S CMyClass::Array[] =
> >>> {
> >>> {5, "Hi"}
> >>> };
> >>>
> >>>
> >>>
> >>> int CMyClass::i[] = {0};
> >>>
> >>>
> >>>
> >>> //////////////////////////////////////////////////////////////////////
> >>> // Construction/Destruction
> >>> //////////////////////////////////////////////////////////////////////
> >>>
> >>> CMyClass::CMyClass()
> >>> {
> >>>
> >>> }
> >>>
> >>> CMyClass::~CMyClass()
> >>> {
> >>>
> >>> }
> >>>
> >>> With this code, Visual C++ 6 (SP6) complier gives following errors.
> >>>
> >>> \MyClass.cpp(10) : error C2440: 'initializing' : cannot convert from
> >> 'const
> >>> int' to 'struct CMyClass::S'
> >>> No constructor could take the source type, or constructor
> >>> overload
> >>> resolution was ambiguous
> >>> MyClass.cpp(10) : error C2440: 'initializing' : cannot convert from
> >>> 'char
> >>> [3]' to 'struct CMyClass::S'
> >>> No constructor could take the source type, or constructor
> >>> overload
> >>> resolution was ambiguous
> >>>
> >>> Even a try of almost one hour, I am unable to know what is wrong with
> >>> this
> >>> code. This code works fine if we replace string objects in S struct
with
> >>> char *. Cany anybody help?
> >>>
> >>>
> >>
> >>
> >
> >
>
>



Relevant Pages

  • Re: How to initialize a member struct
    ... I can't give this struct a constructor. ... class constructor (call it "TemplatedClass") in another DLL library. ... This leaves me with trying to come up with a way to initialize ...
    (microsoft.public.dotnet.languages.vc)
  • Re: C++ Compiler behavior regarding struct constructors
    ... > struct default constructor ... underlying class type shall have a user-declared default constructor. ... MyStruct, that default constructor zero-fills the memory. ... if you need initialization, ...
    (microsoft.public.dotnet.languages.vc)
  • [PATCH 7/11] PPC64: remove debug_switch from the naca
    ... The patch moves the debug_switch from the naca to a global variable. ... struct ppc64_caches ppc64_caches; ... -static void __init initialize_naca ...
    (Linux-Kernel)
  • Re: TREE structure in MFC, how, where and why?
    ... Your struct contains a CString object, which is a C++ class with various ... By using malloc, the constructor on your CString is not being called. ...
    (microsoft.public.vc.mfc)
  • Re: remove structs default constructor?
    ... I can only initialize those private members via constructor ... if the default constructor does not disappear once I ... > create my own struct constructor, the caller can create a struct still ... Imagine only having proper values. ...
    (microsoft.public.dotnet.languages.csharp)