I am having a bit of trouble with C++ classes. I am writing a class (cSpline) to do a cubic spline fit on a set of input points. I want to use a cSpline object within another class (ss304, which provides properties of type 304 stainless steel as a function of temperature). I have four files. Here is the code I have come up with:
cSpline.h:
class cSpline {
private:
double *setCoeffs(double* x_in, double* f_in, int size);
double *coeffs;
double *f;
double *x;
int sizex;
public:
double *interpolate(double* y, int sizey);
cSpline(double* x_in, double* f_in, int size);
};
cSpline.cpp:
cSpline::cSpline(double* x_in, double* f_in, int size) {
f = f_in;
x = x_in;
sizex = size;
coeffs = setCoeffs(x, f, size);
}
double* cSpline::setCoeffs (double* x_in, double* f_in, int size) {
double *ypp = new double[size];
for (int i = 0; i < size; i++) {
ypp[i] = 0.0;
}
return ypp;
}
double* cSpline::interpolate(double* y, int sizey){
double *g = new double[sizey];
for (int i = 0; i < sizey; i++) {
g[i] = 0.0;
}
return g;
}
ss304.h:
#include "cSpline.h"
class SS304 {
private:
// Material constants
static const double T0 = 273.0; // standard temp, K
static const double rho0 = 7924.0; // density at STP, kg/m^3
static const double Tm = 1700.0; // melting temp, K
static const double Hm = 265.3+03; // heat of melting, J/kg
static const double rhom = 7015.0; // density of molten phase at Tm (iron), kg/m^3/K
static const double drdt = -0.883; // temperature coefficient of densiy at Tm, kg/m^3/K
static const double Tv = 3100.0; // vaporization temperature, K
static const double Hv = 6.258e+06; // heat of vaporization, J/kg
// Property Array Sizes
static const int Na1 = 10;
// Property Arrays
double alpha1[Na1]; //thermal expansivity, T0 < T < Tm, 1/K
double Talpha1[Na1];
cSpline csalpha1(double* x, double* f, int size);
public:
double* alpha;
void setProp1D(double* T, int size);
SS304();
};
ss304.cpp:
#include "ss304.h"
SS304::SS304() {
double alpha1[Na1] = {13.6e-6, 16.1e-6, 17.15e-6, 17.8e-6, 18.65e-6, 19.2e-06, 19.85e-06, 20.55e-06, 20.9e-06};
double Talpha1[Na1] = { 200., 400., 500., 600., 700., 800., 1000., 1200., 1400.};
cSpline csalpha1(Talpha1, alpha1, Na1);
}
void SS304::setProp1D(double* T, int size) {
double* alpha = new double[size];
alpha[0] = csalpha1.interpolate(T[0]);
}
What I am trying to accomplish here is this: Upon creation of a ss304 object, I set the properties of 304 stainless, in this case alpha1, at a given set of temperatures, Talpha1. Then I create a cSpline object, csalpha1, for later use. Creation of the cSpline object goes ahead and calculates the spline coefficients. Then, when I call SS304::setProp1D with an array of temperatures, it should set the values of alpha[] based on an interpolation of the cubic spline at each temperature in T[]. Obviously, I left out the full implementation of the spline for the sake of space, but the implementation is irrelevant to the error I get, which is:
ss304.cpp: In member function ‘void SS304::setProp1D(double*, int)’:
ss304.cpp:12: error: ‘((SS304*)this)->SS304::csalpha1’ does not have class type
So, I think I have some basic misunderstanding of how exactly classes work in C++. I think I am trying to use them as I do in Python, where I have this working just fine. But, obviously I am missing something in C++. I have googled around quite a bit, but not found any help that I understood. Any help would be greatly appreciated.