Create arrays with constructor problem

This is a discussion on Create arrays with constructor problem within the C Programming forums, part of the General Programming Boards category; Hello, I think the title i snot the best. Here is my problem: Code: IntArray v(7); // creates an integer ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    6

    Create arrays with constructor problem

    Hello,

    I think the title i snot the best. Here is my problem:
    Code:
    IntArray v(7);  // creates an integer array with size 7
    IntArray v[10]; // creates an array with 10 elements where each element is an integer array (with size 0)
    I now want to connect these both and want to create an array with 10 IntArray elements, where every IntArray has size 7. Is this possible?

    Best greetings and thanks for the help,
    Dee

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is no such variable type as 'IntArray'. If you've typedef-ed one, then you need to show us what it is. If it's a macro, show that too. Otherwise, you're wrong on all accounts.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    Hello!

    Unfortunately I think I've posted in the wrong board. It should be a C++ problem! Some mod may be so nice and move it please.

    IntArray is just a class with something like int *A[]... only that the boundaries are checked before.

    At the end I just want to have something like int *q[7][10] but with my IntArray.

    Greetings,
    Dee
    Last edited by dee.dw; 10-26-2005 at 08:06 AM.

  4. #4
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    what is [b]???? Maybe its me but I don't get your post. if you want to set something like int q[7][10] through a constructor, pass it 2 integers and make those your indices. Some code would have helped wonders.

    Code:
    class IntArray
    {
    public:
      IntArray(int a, int b);
      ~IntArray();
    private:
      int* array;
      int rows;
    };
    
    IntArray::IntArray(int a, int b)
    {
      array = new int[a][b];
      rows = a;
    }
    
    IntArray::~IntArray()
    {
      for( int i=0; i<rows; i++ )
      {
         if( array[i] )
         {
           delete [] array[i];
           array[i] = 0;
         }
      }
      delete [] array;
    } // always cleanup :)
    Last edited by durban; 10-26-2005 at 06:57 AM.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  5. #5
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    I didn't even know C had constructors.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    @durban: The [b] was vB-Code. Should be bold, but I correct it now... I hope the meaning is now a little bit clearer.

    And you want code, so I post some:
    Code:
    class IntArray {              // IntArray
    
    public:
      IntArray(void);                       // standard constructor
      IntArray(const unsigned int l);       // standard constructor
    
    private:
      int* values;                  // array of integer for values
      unsigned int length;        // size of array
    };
    
    IntArray::IntArray(void){    // standard constructor
      length=0;               // set length of array for better indexing
      values=NULL;
    };
    
    IntArray::IntArray(const unsigned int l){  // standard constructor
    
      length=l;               // set length of array for better indexing
      if (length) {
         values=new int[l];
         for (unsigned int i=0; i<length; i++) values[i]=0;
      }
      else values=NULL;
    };
    
    IntArray::~IntArray() {             // destructor
      if (values) delete[] values;    // give memory free
    };
    Your code is good, I only thought that there is a solution in C or C++ that gives me the same with one line, because with
    Code:
    IntArray v=IntArray[10]
    my first constructor is called and set the length to 0.

    So my question again: Is it possible to call the second constructor with a number l?

    @Learner: As I said in my second post I've chosen the wrong message board.

    Sorry for all the chaos...

    Greetings, Dee

  7. #7
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Ah, i think i see your problem. Unfortunately, 1 line obviously can't do this because you would want different l values for each IntArray right?

    Code:
    IntArray* v[10];
    v[0] = new IntArray(5);
    v[1] = new IntArray(10);
    etc..
    Try that code
    Last edited by durban; 10-26-2005 at 08:42 AM.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    Different sizes in each entry as you said would be the next. And I think the only way to init these arrays is with a ingle initialisation as you mentioned it...

    At this time I do it as:
    Code:
    Int Array *v[10];
    for (int i=0; i<10; i++) v[i]=new IntArray(5)
    But I thought you could it do shorter... Something like
    Code:
    Int Array v(5)[10]
    that creates 10 IntArrays with size 5.

    Greetings, Dee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create two arrays from command line and work on them
    By simpatico_qa in forum C Programming
    Replies: 30
    Last Post: 03-14-2009, 10:37 AM
  2. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  3. dynamic memory and arrays
    By jomns in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2004, 01:18 PM
  4. about arrays create within a struct~
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-26-2002, 04:21 AM
  5. writing arrays to files
    By Zaarin in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21