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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

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


Date: Thu, 23 Dec 2004 16:16:09 +0800

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