473,506 Members | 14,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create an array which can store two types of data

5 New Member
Hello, fellows:

I having difficulties on creating an array which can store two different data types, for example: string and int, in C++. Some of the websites told me it is impossible. Or use class to do it. Could anyone give me a hand on this. Thank you every much!
Apr 4 '07 #1
10 8590
Banfa
9,065 Recognized Expert Moderator Expert
A class or structure is the way to go, you could start by reading this.
Apr 4 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
Do you know about class templates yet?

template<class T>
class Data
{
T theValue
//class methods omitted for brevity
};

Data<int> arr[100]; //array of 100 Data objects containing ints
Data<string> arr1[100]; //array of 100 Data objects containing strings
Apr 5 '07 #3
rakchha
1 New Member
Hello, fellows:

I having difficulties on creating an array which can store two different data types, for example: string and int, in C++. Some of the websites told me it is impossible. Or use class to do it. Could anyone give me a hand on this. Thank you every much!
An indirect way to do this is to create an array of struct which itself is union of two or more data types eg

union u
{
int i;
char j; //for storing string u can keep char *
} ;

struct s
{
int eletype; //say int =0, char =1 and so on
union u data;
};

struct s array[10];
array[0].eletype = 0; //store int
array[0].data = 12;

array[1].eletype = 1;
array[1].data = 'g';
Apr 5 '07 #4
cj179
5 New Member
Thanks for the reply and I have found them being very helpful to me.

But I have a question on the Class template approach, is it still using two arrays rather than one. because I have to define two arrays when I instantiate an object of this class. Have I misunderstand this method?

Thanks to everyone again.
Apr 9 '07 #5
cj179
5 New Member
When I implementing the struct mehod in DEV c++ , I got this error as below:

24 E:\new c++\c++290307\ltest1.cpp no match for 'operator=' in 'array[0].main()::s::data = 12'

Does it mean I have to define a operator in the class, how to do it?

thanks
Apr 9 '07 #6
Banfa
9,065 Recognized Expert Moderator Expert
When I implementing the struct mehod in DEV c++ , I got this error as below:

24 E:\new c++\c++290307\ltest1.cpp no match for 'operator=' in 'array[0].main()::s::data = 12'

Does it mean I have to define a operator in the class, how to do it?

thanks
May be seeing the code line would help.
Apr 9 '07 #7
cj179
5 New Member
Hi, here is the code with error I mentioned early on:

#include <iostream>
#include <stdlib.h>
#include <string>


using namespace std;

int main(){
union u
{
int i;
char j; //for storing string u can keep char *
} ;

struct s
{
int eletype; //say int =0, char =1 and so on
union u data;
};

struct s array[10];
array[0].eletype = 0; //store int
array[0].data = 12;

array[1].eletype = 1;
array[1].data = 'g';


system("PAUSE");

}
Apr 9 '07 #8
Banfa
9,065 Recognized Expert Moderator Expert
array[0].data = 12;
array[0].data is of type union u and does not support the = operator (unless you write a function for it, you probably want 1 of these 2 lines

Expand|Select|Wrap|Line Numbers
  1. array[0].data.i = 12;
  2. array[0].data.j = 12;
  3.  
Apr 9 '07 #9
cj179
5 New Member
Thank you for the reply. If it is possible, could you give me some hints for how to write this function for =operator?
Thank you
Apr 10 '07 #10
Banfa
9,065 Recognized Expert Moderator Expert
Read This.
Apr 10 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1985
by: Ross Micheals | last post by:
All I have some general .NET questions that I'm looking for some help with. Some of these questions (like the first) are ones that I've seen various conflicting information on, or questions that...
6
2355
by: billy | last post by:
I've got a set of subclasses that each derive from a common base class. What I'd like to do is create a global array of the class types (or, class names) that a manager class can walk through in...
7
8822
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
48
2117
by: yezi | last post by:
Hi, all: I want to record some memory pointer returned from malloc, is possible the code like below? int memo_index; int i,j; char *tmp; for (i=0;i<10;i++){
3
3267
by: Mateusz [PEYN] Adamus | last post by:
Hi. I have a problem. I'm creating a structure - looks like this: <c#> internal struct TwEnumeration { public ushort ItemType; public uint NumItems;
11
3412
by: mesut demir | last post by:
Hi All, When I create fields (in files) I need assign a data type like char, varchar, money etc. I have some questions about the data types when you create fields in a file. What is the...
17
7211
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
11
4615
by: memeticvirus | last post by:
I have an array cli::array<float, 2and I would like to access a subset of it's values by compiling an array of pointers. But, it's not possible to create an array of type...
9
5150
by: ssubbarayan | last post by:
Hi all, I am trying a program to convert floating point values to a byte array and printing the same to the screen.The idea behind this is we already have an existing function which can do byte...
0
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7371
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...
1
7023
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
7479
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
5617
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,...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.