-3

I don't know how to initialize array through constructor. I know that one way to initialize array to all 0 values is the one from here How to initialize all elements in an array to the same number in C++ However, I DO need to follow the convention you see in my code. I need setArr() and getArr() aswell as constructor. Can you please tell me, what to put for constructor and those functions, so that arr[5] will work correctly, just as i works? I will really appreciate your explanation, as I did not find example of such initialization in constructor for array. Thank you, regards

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
class A
{
private:
    double arr[5];
    int i;
public:
    //THIS HERE IS WRONG, BUT HOW TO PROGRAMM IT SO THAT I GET NO ERRORS?
    A( double arrx = {0}, int ix = 4):, i(ix) 
    {
        std::vector<double> v1(arrx, arrx+5);
        std::fill(v1.begin(arr), v1.end(arr), arrx);
    }
    ~A() {}
    void setI( int ix ) { i = ix; }
    double getI(void) { return i; }
    void setArr( double arrx[] ) 
    {
        for (int i=0; i < sizeof(arrx); i++)
            arr[i] = arrx[i];
    }
    double* getArr(void) {  return arr; }
};

int main()
{
    A ob1(6);
    //ob1.setI(5);
    std::cout << ob1.getI() << std::endl;
}

EDIT: I will update the code up to when it works so that the others can benefit from it later on. I corrected and get error C2661: 'std::vector<_Ty>::begin' : no overloaded function takes 1 arguments

Community
  • 1
  • 1
beginh
  • 1,133
  • 3
  • 26
  • 36
  • 1
    Start correcting your code by using correct C++ syntax. – 101010 Jun 06 '14 at 07:50
  • The idiomatic way of initializing al array elements to `0` is [shown here](http://stackoverflow.com/questions/23987515/zero-initializing-an-array-data-member-in-a-constructor/23987926#23987926). – juanchopanza Jun 06 '14 at 08:34
  • @juanchopanza the constructor seems to be attempting to initialise all elements to whatever the `arrx` parameter is - it's not clear whether it's meant to be limited to a single value to be used 5 times, and/or whether it's meant to be able to accept an actual 5-value array - but whichever of those is wanted, it doesn't appear to want unconditional initialisation to 0 as covered in your link... – Tony Delroy Jun 06 '14 at 08:49
  • @TonyD I know, I was explaining about setting everything to `0`, since that is what OP leads with, that's all. I've already been here though. There was an almost identical question not too long ago. – juanchopanza Jun 06 '14 at 08:57

1 Answers1

3

First of all in my opinion your class design has no any sense. :) Nevertheless I think you need at least tthree (or even four) constructors: the default constructor, a constructor with an initializer list and a constructor with two parameters. For example

#include <iostream>
#include <initializer_list>
#include <algorithm>
#include <iterator>
#include <stdexcept>

class A
{
private:
    static const size_t MAX_SIZE = 5;  
    double arr[MAX_SIZE];
    size_t i;
public:
    A() : arr{}, i( MAX_SIZE ) {}

    A( size_t n, double x = 0.0 ) : arr{}, i( n < MAX_SIZE ? n : MAX_SIZE )
    {
        std::fill( arr, arr + i, x );
    }

    A( double a[], size_t n ) 
        : arr{}, i( n < MAX_SIZE ? n : MAX_SIZE )
    {
        std::copy( a, a + i, arr );
    }

    A( std::initializer_list<double> l ) 
        : arr{}, i( l.size() < MAX_SIZE ? l.size() : MAX_SIZE )
    {
        std::copy( l.begin(), std::next( l.begin(), i ), arr );
    }

    void SetValue( double value, size_t n )
    {
        if ( n < i ) arr[n] = value;
    }

    double GetValue( size_t n ) const throw( std::out_of_range )
    {
        if ( i <= n ) throw std::out_of_range( "A::GetValue" );
        return ( arr[n] );
    }

    size_t GetI() const
    {
        return i;
    }
};

int main() 
{
    A a = { 1.0, 2.0, 3.0, 4.0, 5.0 };

    for ( size_t i = 0; i < a.GetI(); i++ ) std::cout << a.GetValue( i ) << ' ';
    std::cout << std::endl;

    try
    {
        a.GetValue( 5 );
    }
    catch( const std::out_of_range &e )
    {
        std::cout << e.what() << std::endl;
    }

    return 0;
}

The output is

1 2 3 4 5 
A::GetValue

EDIT: If your compiler does not support ctor-initialization with initializer list then instead of the two constructors

    A() : arr{}, i( MAX_SIZE ) {}

    A( size_t n, double x = 0.0 ) : arr{}, i( n < MAX_SIZE ? n : MAX_SIZE )
    {
        std::fill( arr, arr + i, x );
    }

you can define one constructor. For example

A( size_t n = MAX_SIZE, double x = 0.0 ) : i( n < MAX_SIZE ? n : MAX_SIZE )
{
    std::fill( arr, arr + i, x );
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Spasiba! Finally someone got my problem: what I am looking for is now why THIS doesn't work for me: *A() : arr{}, i( MAX_SIZE ) {}* – beginh Jun 06 '14 at 08:49
  • @beginh It is posiible that your compiler does not support ctor initialization with initializer list. – Vlad from Moscow Jun 06 '14 at 08:51
  • it does, but when I try to use such approach for array, not only simple variables, it doesn't. So i am ALL THE TIME wondering how to init the array in the given convention of the class, which doesn't come from me. Why do you think it has no sense by the way? – beginh Jun 06 '14 at 08:53
  • @beginh The code I showed is compiled with GCC successfully. See also my updated post. – Vlad from Moscow Jun 06 '14 at 08:57
  • (Sorry Vlad - accidentally edited your question - have rolled back) – Tony Delroy Jun 06 '14 at 09:06