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!
10 8590 Banfa 9,065
Recognized Expert Moderator Expert
A class or structure is the way to go, you could start by reading this.
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
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';
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.
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
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.
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");
}
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 -
array[0].data.i = 12;
-
array[0].data.j = 12;
-
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
Banfa 9,065
Recognized Expert Moderator Expert Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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++){
|
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;
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |