473,396 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

array of structures initialization

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
Jul 22 '05 #1
3 2659
Steve wrote in news:I7********@news.boeing.com 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
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" <rt*@freenet.co.uk> wrote in message
news:Xn**********************************@130.133. 1.4...
Steve wrote in news:I7********@news.boeing.com 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.
--
http://www.victim-prime.dsl.pipex.com/

Jul 22 '05 #3
Steve wrote:
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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
20
by: K.M. Jr. | last post by:
Hi all, Consider this line - char s = "\0"; Does this initialize all array elements to zero ? Thanks.
7
by: Chandrashekar Tippur | last post by:
All, I need to initialize an array of structures but I don't know how may elements are there. I tried to malloc the array but I am not sure how to initialize them. Snippet: struct myarray{...
2
by: Christopher Benson-Manica | last post by:
Is the following program conforming under C99? #include <stdio.h> typedef struct foo { int bar; int baz; } foo; foo foos={
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
2
by: mahesh_cbrl | last post by:
Hi, I am unable to intialize an array of structures and I keep getting an error. Here is how the data structure looks like. typedef struct { struct { int part_no; char item_name;
8
by: ulyses | last post by:
I'm trying to put pointer to flexible array of structures in other structure. I want to have pointer to array of pixels in screen structure. Here is mine code, but I think it isn't quite all right:...
2
by: leatherbrain | last post by:
Is there any way I can initialize the value of a single member of a structure in the declaration of an array of these structures? Eg. struct StructEx { int a; int b; }
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.