I need some guidance. I am trying to build a class B2
that has among its members an object from class A2
. Class A2
on the other hand allocate some memory through the member pointer '*y' in a dynamical fashion. Here is my code that right now does not compile.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
class A2{
int N;
public:
double *y;
A2(int N_);
~A2(){free(y);};
};
A2::A2(int N_){
N = N_;
y = (double*)calloc(N,sizeof(double));
}
class B2{
int N;
public:
A2 obj(N);
B2(int N_) : N(N_) {};
~B2(){};
};
int main(){
int N = 10;
B2 model(N);
for(int i=0;i<N;i++) model.obj.y[i] = i;
for(i=0;i<N;i++) printf("\ny[%d]=%d",i,model.obj.y[i]);
return 1;
}
When I try to initialize model
I get the error: ‘N’ is not a type
in the initialization of A2 obj(N)
this is why I thought to use a member initializer list as suggsted by this post.