开发者

C++ Error: does not have a class type

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 t开发者_开发知识库o 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.


You can't initialize a data member in the constructor by calling the data member's constructor. You will need an assignment or initialization method for the object:

SS304::SS304()
{
    static const 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};
    static const double Talpha1[Na1] = {200.,    400.,     500.,
                                        600.,    700.,     800.,
                                       1000.,   1200.,    1400.};
    csalpha1.initialize(Talpha1, alpha1, Na1);
} 

Also, familiarize yourself with initialization lists and the C++ FAQ (which can be found by searching the web).

I converted the arrays into static const to force the compiler to place the data into Read-Only Memory and to access it from the Read-Only Memory (versus copying the data onto the stack).

The compiler will use a default or empty constructor when creating data members of a class.


static const double T0 =     273.0; // standard temp, K
static const double rho0 =  7924.0; // density at STP, kg/m^3

Wrong. In C++, static const double cannot be initialized in the class-definition itself. Only static integral type can be initialized inside the class.

Also, you cannot initialize member arrays in the constructor like you've done. That is also wrong. In fact, there is no way you can initialize them. However, you can do this:

SS304::SS304() 
{
  double local_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 local_Talpha1[Na1] = {   200.,    400.,     500.,    600.,     700.,     800.,     1000.,     1200.,    1400.};

  std::copy(local_alpha1, local_alpha1 + Na1, alpha1);
  std::copy(local_Talpha1, local_Talpha1+ Na1, Talpha1);
}

Now I would suggest you to read some good book first, because I feel that there are lots of basic things you need to know before writing classes in C++. Here is a list of some recommended books; choose any introductory book:

  • The Definitive C++ Book Guide and List


It seems to me that cSpline csalpha1(double* x, double* f, int size); is a function, so you should call it then call interpolate on the return value:

void SS304::setProp1D(double* T, int size) {
  double* alpha = new double[size];
  alpha[0] = csalpha1(..., ..., ...).interpolate(T[0]);
}


What you have is a function declaration:

cSpline csalpha1(double* x, double* f, int size);

I guess you wanted a member variable so use cSpline csalpha1;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜