namespace and includes causing compiler issues.

I am trying to compose a 1 dimension function evaluation tool for a school project. I am pretty new to C++ and am getting some compiler errors.
Function1D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Function1D.h"

using namespace csc350Lib_calc_base;

Function1D::Function1D(void) {
}

Function1D::~Function1D(void) {
}

float Function1D::dfunc(float x) {
    return x;
}

float Function1D::dfunc(float x) {
    return (func(x+0.01f) - func(x))/0.01f; // no implementation of the Richardson algorithm...
}

Function1D.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef FUNCTION1D_H
#define	FUNCTION1D_H

namespace csc350Lib_calc_base
{
    class Function1D {
    public:
        Function1D(void);                     // see [1] below
        virtual ~Function1D(void);             // see [2] below
        virtual float func(float x) = 0;          // see [3] below
        virtual float dfunc(float x);          // see [4] below
        virtual bool isExactDerivativeDefined(void) = 0;  // see [5] below
        //[1]: Default constructor. No required behavior (as far as I am concerned, it could contain no code at all).
        //[2]: Destructor. Always declare virtual the constructor of a class that contains a pure virtual function, even if you actually provide an implementation for the destructor. This takes care of potential problems related to polymorphism.
        //[3]: This pure virtual function will be implemented by the subclasses.
        //[4]: This method should return the approximate value of your function's first derivative at x. This value will be computed using the Richardson extrapolation algorithm. Subclasses may overload this method, using a hard-coded expression for the exact derivative.
        //[5]: This method, to be overloaded (and hard-coded) by subclasses, returns true if the subclass implements the exact derivatives, and false. if the value returned by dfunc is computed using the Richardson extrapolation.
    private:   // you're on your own
    };
}


#endif	/* FUNCTION1D_H */

PolyFunction1D.cpp

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
28
29
30
31
32
33
34
35
36
37
38
#include <cmath>
#include <math.h>
#include "PolyFunction1D.h"
using namespace csc350Lib_calc_base;

PolyFunction1D::PolyFunction1D() : Function1D()
{
}

PolyFunction1D::~PolyFunction1D()
{
}

float PolyFunction1D::func(float f[], float x) {
    return evalPoly(f, x);
}

float PolyFunction1D::dfunc(float f[], float x) {
    int arrSize = sizeof(f);
    float coeffDF[arrSize-1];
    for (int i = 0; i<= arrSize-1;i++)
    {
        coeffDF[i] = f[i+1];
    }
    return evalPoly(coeffDF, x);
}

float evalPoly(float f[], float x)
{
    int arrSize = sizeof(f);
    float total = 0;
    for (int i = 0; i <= arrSize; i++)
    {
        total = total + (f[i] * pow (x,i));
    }
    
    return total;
}

PolyFunction.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef PLOYFUNCTION1D_H
#define	PLOYFUNCTION1D_H

namespace csc350Lib_calc_base
{
    class PolyFunction1D : public Function1D
    {
    public:
        PolyFunction1D(void);
        ~PolyFunction1D();
        float func(float f[], float x);
        float dfunc(float f[], float x);
        float evalPoly(float f[], float x);
    private:
    };
}
#endif	/* PLOYFUNCTION1D_H */

SimpleFunction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cmath>
#include "SimpleFunction.h"
using namespace csc350Lib_calc_base;

SimpleFunction::SimpleFunction() : Function1D() {
}


SimpleFunction::~SimpleFunction() {
}

float SimpleFunction::func(float x) {
    return x*x*cosf(x);
}

SimpleFunction.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef SIMPLEFUNCTION_H
#define	SIMPLEFUNCTION_H


namespace csc350Lib_calc_base {
class SimpleFunction : public Function1D {
public:
    SimpleFunction(void);

    ~SimpleFunction();

    float func(float x);

private:

};
}
#endif	/* SIMPLEFUNCTION_H */

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdlib>
#include <iostream>
#include "Function1D.h"
#include "SimpleFunction.h"

using namespace std;
using namespace csc350Lib_calc_base;
int main(int argc, char** argv) {
    
    return 0;
}


I know this is a lot of code to read and review but any help with the following errors would be greatly appreciated.
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/cygdrive/c/Users/Dennis/Documents/NetBeansProjects/CSC350Assignment1'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin_4.x-Windows/csc350assignment1.exe
make[2]: Entering directory `/cygdrive/c/Users/Dennis/Documents/NetBeansProjects/CSC350Assignment1'
mkdir -p build/Debug/Cygwin_4.x-Windows
rm -f build/Debug/Cygwin_4.x-Windows/PolyFunction1D.o.d
g++.exe -c -g -MMD -MP -MF build/Debug/Cygwin_4.x-Windows/PolyFunction1D.o.d -o build/Debug/Cygwin_4.x-Windows/PolyFunction1D.o PolyFunction1D.cpp
In file included from PolyFunction1D.cpp:3:
PolyFunction1D.h:7: error: expected class-name before ‘{’ token
PolyFunction1D.cpp: In constructor ‘csc350Lib_calc_base::PolyFunction1D::PolyFunction1D()’:
PolyFunction1D.cpp:6: error: class ‘csc350Lib_calc_base::PolyFunction1D’ does not have any field named ‘Function1D’
make[2]: Leaving directory `/cygdrive/c/Users/Dennis/Documents/NetBeansProjects/CSC350Assignment1'
make[1]: Leaving directory `/cygdrive/c/Users/Dennis/Documents/NetBeansProjects/CSC350Assignment1'
make[2]: *** [build/Debug/Cygwin_4.x-Windows/PolyFunction1D.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 1s)


Compiled in netbeans with cygwin.

Thanks for the help everyone.

-DG

PolyFunction1D.h has to include Function1D.h to get the Function1D symbol.
(which is what the first compile error says)
Also, I believe that your implementation needs to be included in the namespace to avoid possible ambiguity.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// A.h
#ifndef A_H
#define A_H

namespace my_std
{

  class A
  {
  public:
    void doSomething(void) const;
  };

} // end namespace

#endif 

1
2
3
4
5
6
7
8
9
10
11
12
// A.cpp

#include "A.h"

namespace my_std
{

  void A::doSomething() const
  {
  }

}
Hey jsmith,

That makes sense. I went and included what you said. Here is a copy of my PolyFunction1D.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef PLOYFUNCTION1D_H
#define	PLOYFUNCTION1D_H

#include "Function1D.h"

namespace csc350Lib_calc_base
{
    class PolyFunction1D : public Function1D
    {
    public:
        PolyFunction1D(void);
        ~PolyFunction1D();
        float func(float f[], float x);
        float dfunc(float f[], float x);
        float evalPoly(float f[], float x);
    private:
    };
}
#endif	/* PLOYFUNCTION1D_H */


I still seem to be getting errors. The errors are this:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/cygdrive/c/Users/Dennis/Documents/NetBeansProjects/CSC350Assignment1'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin_4.x-Windows/csc350assignment1.exe
make[2]: Entering directory `/cygdrive/c/Users/Dennis/Documents/NetBeansProjects/CSC350Assignment1'
mkdir -p build/Debug/Cygwin_4.x-Windows
rm -f build/Debug/Cygwin_4.x-Windows/SimpleFunction.o.d
g++.exe -c -g -MMD -MP -MF build/Debug/Cygwin_4.x-Windows/SimpleFunction.o.d -o build/Debug/Cygwin_4.x-Windows/SimpleFunction.o SimpleFunction.cpp
In file included from SimpleFunction.cpp:2:
SimpleFunction.h:6: error: expected class-name before ‘{’ token
SimpleFunction.cpp: In constructor ‘csc350Lib_calc_base::SimpleFunction::SimpleFunction()’:
SimpleFunction.cpp:5: error: class ‘csc350Lib_calc_base::SimpleFunction’ does not have any field named ‘Function1D’
make[2]: *** [build/Debug/Cygwin_4.x-Windows/SimpleFunction.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
make[2]: Leaving directory `/cygdrive/c/Users/Dennis/Documents/NetBeansProjects/CSC350Assignment1'
make[1]: Leaving directory `/cygdrive/c/Users/Dennis/Documents/NetBeansProjects/CSC350Assignment1'

BUILD FAILED (exit value 2, total time: 1s)

It's going to be the same deal as before, you need to include Function1D.h in your SimpleFunction.h.

"PolyFunction1D.cpp: In constructor ‘csc350Lib_calc_base::PolyFunction1D::PolyFunction1D()’:
PolyFunction1D.cpp:6: error: class ‘csc350Lib_calc_base::PolyFunction1D’ does not have any field named ‘Function1D’"

"SimpleFunction.cpp: In constructor ‘csc350Lib_calc_base::SimpleFunction::SimpleFunction()’:
SimpleFunction.cpp:5: error: class ‘csc350Lib_calc_base::SimpleFunction’ does not have any field named ‘Function1D’"
Topic archived. No new replies allowed.