array of structures initialization

S

Steve

A coworker just returned to C++ coding after a long hiatus and asked me how
to accomplish this task. What seemed liked simple issue, we have been unable
to resolve. He has declared a structure that includes an MFC class and wants
to initialize it at declaration time. Something like this:

In the h. file:

namespace SomeNamespace
{

struct SomeStruct
{
long theNumber;
CPoint thePoint;
};

SomeStruct arrssTheArray[] =
{
{
1,
CPoint(1,1)
},
{
2,
CPoint(2,2)
}
};
};

I have about 5 years experience in C\C++ but am self-taught so I can't rely
on any training background and I have been unable to find a similar example
of code. The compiler compliains that is cannot cast the long to a
"SomeStruct".

We also tried:

In the .h file:

namespace SomeNamespace
{

struct SomeStruct
{
long theNumber;
CPoint thePoint;
};

SomeStruct arrssTheArray[];
};

In the .cpp file:

#include "SomeFile.h"

SomeNamespace::SomeStruct SomeNamespace::arrssTheArray[] =
{
{
1,
CPoint(1,1)
},
{
2,
CPoint(2,2)
}
};

In this case the compiler complains about a redefinition. Can this task even
be done? Where can one go for information of this sort? I have about ~4
books on C\C++ but cannot seem to find a definitive answer for issues like
this. Thanks all.

Steve
 
R

Rob Williscroft

Steve wrote in in comp.lang.c++:
A coworker just returned to C++ coding after a long hiatus and asked
me how to accomplish this task. What seemed liked simple issue, we
have been unable to resolve. He has declared a structure that includes
an MFC class and wants to initialize it at declaration time. Something
like this:

#include <iostream>
#include <utility>

/** Simple hack to post *compilable* code
*/
typedef std::pair< int, int > CPoint;

namespace SomeNamespace
{
struct SomeStruct
{
long theNumber;
CPoint thePoint;
};

SomeStruct a[] =
{
{ 1, CPoint(1,1) },
{ 2, CPoint(2,2) }
};

/** Needs to extern otherwise its a *defenition*
*/
extern SomeStruct b[];
}

/** NOT a redefenition as the above is only a declaration
*/
SomeNamespace::SomeStruct SomeNamespace::b[] =
{
{ 1, CPoint(1,1) },
{ 2, CPoint(2,2) }
};

int main()
{
std::cout << SomeNamespace::a[1].thePoint.first << '\n';
std::cout << SomeNamespace::b[1].thePoint.first << '\n';
}

The above compiles fine with every compiler I tried, including
MSVC 7.1.

HTH.

Rob.
 
S

Steve

Thank you Rob, but no dice... exact code I used:

namespace Something
{
typedef std::pair< int, int > CPoint;

struct SomeStruct
{
long theNumber;
CPoint thePoint;
};

extern SomeStruct b[];
}

Something::SomeStruct Something::b[] =
{
{ 1, CPoint(1,1) },
{ 2, CPoint(2,2) }
};

Produces the following error using VC6.

error C2440: 'initializing' : cannot convert from 'const int' to 'struct
JSOWCT::SomeStruct'
No constructor could take the source type, or constructor overload
resolution was ambiguous

????
Thanks again.
Steve


Rob Williscroft said:
Steve wrote in in comp.lang.c++:
A coworker just returned to C++ coding after a long hiatus and asked
me how to accomplish this task. What seemed liked simple issue, we
have been unable to resolve. He has declared a structure that includes
an MFC class and wants to initialize it at declaration time. Something
like this:

#include <iostream>
#include <utility>

/** Simple hack to post *compilable* code
*/
typedef std::pair< int, int > CPoint;

namespace SomeNamespace
{
struct SomeStruct
{
long theNumber;
CPoint thePoint;
};

SomeStruct a[] =
{
{ 1, CPoint(1,1) },
{ 2, CPoint(2,2) }
};

/** Needs to extern otherwise its a *defenition*
*/
extern SomeStruct b[];
}

/** NOT a redefenition as the above is only a declaration
*/
SomeNamespace::SomeStruct SomeNamespace::b[] =
{
{ 1, CPoint(1,1) },
{ 2, CPoint(2,2) }
};

int main()
{
std::cout << SomeNamespace::a[1].thePoint.first << '\n';
std::cout << SomeNamespace::b[1].thePoint.first << '\n';
}

The above compiles fine with every compiler I tried, including
MSVC 7.1.

HTH.

Rob.
 
V

Victor Bazarov

Steve said:
Thank you Rob, but no dice... exact code I used:

namespace Something
{
typedef std::pair< int, int > CPoint;

struct SomeStruct
{
long theNumber;
CPoint thePoint;
};

extern SomeStruct b[];
}

Something::SomeStruct Something::b[] =
{
{ 1, CPoint(1,1) },

Try adding a constructor to your SomeStruct:

struct SomeStruct
{
long theNumber;
CPoint thePoint;
SomeStruct(long N, int i1, int i2)
: theNumber(N), thePoint(std::make_pair(i1,i2)) {}
};

and initialising it like this:

SomeStruct(1, 1, 1)

{ 2, CPoint(2,2) }
};

Produces the following error using VC6.

error C2440: 'initializing' : cannot convert from 'const int' to 'struct
JSOWCT::SomeStruct'
No constructor could take the source type, or constructor overload
resolution was ambiguous

????

And upgrade your compiler to v7.1 ASAP.
Thanks again.
Steve
[...]

And don't top-post.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top