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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cj179
    New Member
    • Apr 2007
    • 5

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

    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!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    A class or structure is the way to go, you could start by reading this.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      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

      Comment

      • rakchha
        New Member
        • Apr 2007
        • 1

        #4
        Originally posted by cj179
        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';

        Comment

        • cj179
          New Member
          • Apr 2007
          • 5

          #5
          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.

          Comment

          • cj179
            New Member
            • Apr 2007
            • 5

            #6
            When I implementing the struct mehod in DEV c++ , I got this error as below:

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

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

            thanks

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by cj179
              When I implementing the struct mehod in DEV c++ , I got this error as below:

              24 E:\new c++\c++290307\l test1.cpp no match for 'operator=' in 'array[0].main()::s::dat a = 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.

              Comment

              • cj179
                New Member
                • Apr 2007
                • 5

                #8
                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") ;

                }

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Originally posted by cj179
                  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

                  Code:
                  array[0].data.i = 12;
                  array[0].data.j = 12;

                  Comment

                  • cj179
                    New Member
                    • Apr 2007
                    • 5

                    #10
                    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

                    Comment

                    • Banfa
                      Recognized Expert Expert
                      • Feb 2006
                      • 9067

                      #11
                      Read This.

                      Comment

                      Working...