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 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/
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/
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
=...
|
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...
|
by: K.M. Jr. |
last post by:
Hi all,
Consider this line -
char s = "\0";
Does this initialize all array elements to zero ?
Thanks.
|
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{...
|
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={
|
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...
|
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;
|
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:...
|
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;
}
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |