I have this code that assigns an array of 8 elements to a pointer that points to a instance j
of a custom structure (Input_Leo
).
In CFNN.cpp:
{
double tx_tech_variable_vec[8] = { 0 }; //I need this re-initialization // code testing: variable rates on the initial premium payment of FIB/Invest Products with a time-variable garantueed interest rate */*/
tx_tech_variable_vec[0] = 3.0; // it does not help to declare it in CFNN.H, as part of the InputLeo structure, you have to do it here before also
tx_tech_variable_vec[1] = 1.5;
DATE tx_tech_variable_dates_vec[8] = { 0, 0 }; // initialization
tx_tech_variable_dates_vec[0] = { 2026, 07 };
tx_tech_variable_dates_vec[1] = { 2032, 07 }; // assignment
j->tx_tech_variable_vec = tx_tech_variable_vec; // overwrites everything (so for each contract (subcontract?), a new vector is written to j)
j->tx_tech_variable_dates_vec = tx_tech_variable_dates_vec; // overwrites everything (so for each contract (subcontract?), a new vector is written to j)
}
In CFNN.H:
struct Input_Leo /* input for Leo */
{
DATE calculation_date = { 2000, 0 }; // Calculation date (used for RADP as date of the payment of unexpected premium)
DATE cohortmonthly = { 2000, 0 }; //date payement prime conclusion (used in total business mode for the month of payment of yearly premiums, to know if we reached 12 months of historic of unex premiums)
int lnumcon = 0; // Contract number
int cregfsc = 0;
and so on...
double* tx_tech = nullptr; // pointer on Tx_res_LEO to allow for dynamic memory allocation, depending on the number of subcontracts
double tx_tech_variable_vec[8] = {0};
DATE tx_tech_variable_dates_vec[8] = {0, 0}; /* code testing not sure I need to add it here */
};
I get this error:
Line Severity Code Description Project File Suppression State
4681 Error C3863 array type 'double [8]' is not assignable CF_2012_V7 C:\Users\U03630\Source\Repos\ctool\Main\code\CFNN.CPP
Line Severity Code Description Project File Suppression State
4682 Error C3863 array type 'DATE [8]' is not assignable CF_2012_V7 C:\Users\U03630\Source\Repos\ctool\Main\code\CFNN.CPP
What am I doing wrong here? Should I specify the length of the array myself explicitly?
Also, the elements should be compatible with the DATE
(div_t type
). Will that still be the case if I work with std::array
or std::vector
?
Side note:
Yes I know this is not the best way of doing it, but yes there are 90000 lines of code in the program I work with, that already use this DATE
structure and this array way of working with things. So just to add a few lines of code, I will not introduce vectors or get rid of the macros or invent another DATE
type that is not compatible with the rest of the (indeed very questionable) code.
C style arrays don't have a copy assignment operator, in C++ you can use std::array which has a copy assignment operator.
#include <array>
struct foo
{
std::array<double,8> arr{}; // zero initialize
};
int main()
{
foo a;
std::array<double,8> b{0,1,2}; // init only first 3 elements
a.arr = b; // all 8 elements copied, including the other value initialized 5 elements
}
you can still treat it like a C stye array using its .data()
method, if you really need to pass around an unsafe double*
if you must use double[]
because you are interfacing with C code then you can use std::copy to copy elements and std::fill to fill elements (for example zero initialization).
#include <algorithm>
#include <array>
struct foo
{
double arr[8]; // struct is used in C code
};
int main()
{
foo a;
// fill array with zeros
std::fill(std::begin(a.arr), std::end(a.arr),0);
double arr2[8] = {1,2,3};
// copy from arr2 to a.arr
std::copy(std::begin(arr2), std::end(arr2), std::begin(a.arr));
}