creating a function with .cc and .hh files

Hi, I'm running into some problems using .cc and .hh files to define a linear interpolation function. Here's what I've got so far:

LinearInterp.hh:
1
2
3
4
5
6
7
8
9
10
#ifndef LinearInterp_h
#define LinearInterp_h

class LinearInterp
{
        public:
        double LinearInterpFunc(double xvec[], double yvec[], double x_in);
};

#endif 


LinearInterp.cc:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

#include "LinearInterp.hh"

using namespace std;

double LinearInterp::LinearInterpFunc(double xvec[], double yvec[], double x_in)
{
        int size = sizeof(xvec)/sizeof(xvec[0]);  //Find length of input vectors, assuming they are the same length

        double y_interp;

        for (int j=0;j<size-1;j++)
        {
                if (x_in >= xvec[j] && x_in < xvec[j+1])  //Find proper sub-interval on x-axis
                {
                        double slope = (yvec[j+1]-yvec[j])/(xvec[j+1]-xvec[j]);  //Calculate slope in proper sub-interval
                        double incpt = yvec[j];                                  //Calculate intercept in proper sub-interval
                        y_interp = slope*(x_in-xvec[j]) + incpt;                 //y=mx+b
                }
        };

        //cout << "y = " << y_interp << endl;

        return y_interp;  //Output is double y_interp
}


interptest.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using namespace std;
#include <iostream>
#include <string>

#include "LinearInterp.hh"

int main()
{
        double xvec[] = {1.85,  2.15,  2.45,  2.75,  3.05,  3.35,  3.65,  3.95,
                         4.25,  4.55,  4.85,  5.15,  5.45,  5.75,  6.05,  6.35,
                         6.65,  6.95,  7.25,  7.55,  7.85,  8.15};

        double yvec[] = {49.80, 39.66, 31.43, 24.79, 19.45, 15.19, 11.80, 9.12,
                         7.01,  5.35,  4.06,  3.06,  2.28,  1.67,  1.21,  0.85,
                         0.58,  0.38,  0.23,  0.13,  0.06,  0.03};

        double y_interp = LinearInterp::LinearInterpFunc(xvec,yvec,2.46);

        cout << y_interp << endl;

        return 0;
}

I'm using make interptest in Terminal on OSX and the error generated is:
1
2
3
interptest.cpp: In function ‘int main()’:
interptest.cpp:17: error: cannot call member function ‘double LinearInterp::LinearInterpFunc(double*, double*, double)’ without object
make: *** [interptest] Error 1



Other times I will get an error asking for a primary expression at line 17 if I change some brackets on the arrays here or there.

Can anyone point out what's going wrong here?
If you don't have a make file, make will assume that interptest is built by linking interptest.o, and interptest.o built by compiling interptest.cpp.

You need a Makefile to tell make that interptest depends on interptest.o and LinearInterp.o. And those are dependent on LinearInterp.hh, and built from interptest.cpp and LinearInterp.cc respectively.

As it happens, make seems to know that .o files can be generated from matching .cpp files, so you don't need to specify that. You need to specify the rest. Here's a simple makefile for you:
1
2
3
4
5
6
all: interptest

interptest: LinearInterp.o interptest.o
	g++ -o $@ $^

LinearInterp.o interptest.o: LinearInterp.hh

Why are you mixing .cpp and .cc for C++ source files?

Oh, a few things.
1. Don't use [] in a function prototype, it's an incomplete type.
2. You'll need to pass the size of the arrays as well as the start of the arrays otherwise you can't tell how large the arrays are in the called function.
3. As you're not instantiating a LinearInterp object to call LinearInterpFunc, and it's not using class data, LinearInterpFunc should be a static member function.
Last edited on
Topic archived. No new replies allowed.