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