Trouble separating Namespaces/classes

I have a project that I got working. But I can't figure out how to seperate all the classes in the namespaces to their respective .h/.cpp files. I don't know if it's the extra strict xcode compiler or if it can't be done or what. Can somebody help?

Main.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <math.h>
#include "Function1d.h"
#include "csc350Lib_calculus_snle.h"

using namespace std;
using namespace csc350Lib_calc_base;
using namespace csc350Lib_calculus_snle;
//---------------------------------------------------------------
class MyFunc1 : public Function1D {
public: 
	float func(float);
	bool isExactDerivativeDefined(void);
	
};


float MyFunc1::func(float x)
{
	return  cos(x+5);
	
}

bool MyFunc1::isExactDerivativeDefined(void)
{
	return false;
}

//---------------------------------------------------------------
class PolyFunction1D : public Function1D{
public: 
	float func(float);
	float dfunc(float);
	bool isExactDerivativeDefined(void);

	PolyFunction1D(int nbCoeffs, const float * coefficients)
	{ 
		numberOfCoeffs = nbCoeffs;
		for (int i =0; i < nbCoeffs; i++) {
			coeff[i] = *(coefficients + i);
		}
	};
private:
	int numberOfCoeffs;
	float coeff[5];
};
bool PolyFunction1D::isExactDerivativeDefined(void)
{
	return true;
}

float PolyFunction1D::func(float x)
{
	switch (numberOfCoeffs) {
		case 0:
			return 0;
			break;
		case 1:
			return coeff[0];
			break;
		case 2:
			return coeff[0] + coeff[1] * x;
			break;
		case 3:
			return coeff[0] + (coeff[1] * x) + (coeff[2] * pow(x, 2));
			break;
		case 4:
			return coeff[0] + (coeff[1] * x) + (coeff[2] * pow(x, 2)) + (coeff[3] * pow(x, 3));
			break;
		case 5:
			return coeff[0] + (coeff[1] * x) + (coeff[2] * pow(x, 2)) + (coeff[3] * pow(x, 3)) + (coeff[4] * pow(x, 4));
			break;
		default:
			return 0;
			break;
	}

	
}
float PolyFunction1D::dfunc(float x)
{
	return (func(x)-func(x-.5))/(x - (x-.5));
}
//-----------------------------------------------------------------
int main (int argc, char * const argv[]) {
	Function1D  *f1, *f2;
    f1 = new MyFunc1();
	float numbers[]= {1,2,3,4,5}; // The coefficients of Polyfunction1D
    f2 = new PolyFunction1D(5, numbers);
	NonlinearSolver *nls;
	nls = new NonlinearSolver_bisection();
	nls->nleSolution = nls->solve(f1, 1., 4., 0.01);
	
    float   x = 3.f;
    float   y1 = f1->func(x),
			y2 = f2->func(x),
		    dy1 = f1->dfunc(x),
			dy2 = f2->dfunc(x);
	
	cout << "Y1 =  "<< y1  << endl;
	cout << "Y2 =  "<< y2  << endl;
	cout << "Dy1 =  " << dy1 << endl;
	cout << "Dy2 =  " << dy2 << endl;
	cout << nls->nleSolution->getSolution() << " - Bisection x " <<endl;
	cout << nls->nleSolution->getValueAtSolution() << " - Value of function at X" << endl;
	cout << nls->nleSolution->getNumberOfIterations() << " - Number of iterations " << endl;
	
	return 0;
}

Function1D.h
1
2
3
4
5
6
7
8
9
10
11
#include <math.h>
namespace csc350Lib_calc_base {
	class Function1D {
	public:
		Function1D(void){};                    
		virtual ~Function1D(void){};             
		virtual float func(float x) = 0;         
		virtual float dfunc(float x);          
		virtual bool  isExactDerivativeDefined(void) = 0;  
	};
}

Function1D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Function1D.h"
namespace csc350Lib_calc_base {
float Function1D::dfunc(float x)  //Richardson Extrapolation 
{	
	float h = 1;
	float D[10][10];
	for (int i =0; i < 10; i++) {
		for (int j = 0; j<= i; j++) {
			if (j==0) {
				D[i][j] = (func(x+h)-func(x))/h;
			}
			else {
				D[i][j] = D[i][j-1] + (D[i][j-1] - D[i-1][j-1])/(pow(4, j) -1);
			}
			
		}
		h = h/2;
	}
	
	return D[9][9];	
	
}//End of dfunc
}

namespace 2
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 *  csc350Lib_calculus_snle.h
 *  CSC350
 *
 *  Created by Los on 2/22/11.
 *  Copyright 2011 Universtity of Rhode Island. All rights reserved.
 *
 */

//-------------------------------------------------------------------------------------
using namespace csc350Lib_calc_base;
namespace csc350Lib_calculus_snle {

	class SolutionNLE{
	public:
		SolutionNLE(){}; //Default constructor
		
		float getSolution(void);          
		float getValueAtSolution(void);  
		int   getNumberOfIterations(void) const; 
		void  setSolution(float);
		void  setFuncValue(float);
		void  setIterations(int);
		~SolutionNLE();
	private:
		float solution;
		float funcValue;
		int   numOfIterations;
		
	};
	void SolutionNLE::setSolution(float answer)
	{
		solution = answer;
	}
	float SolutionNLE::getSolution(void)
	{
		return solution;
	}
	float SolutionNLE::getValueAtSolution(void)
	{
		return funcValue;
	}
	void SolutionNLE::setFuncValue(float answer)
	{
		funcValue = answer;
	}
	int SolutionNLE::getNumberOfIterations(void) const
	{
		return numOfIterations;
	}
	void SolutionNLE::setIterations(int iterations)
	{
		numOfIterations = iterations;
	}
	SolutionNLE::~SolutionNLE(){}
//--------------------------------------------------------------------------------

	class NonlinearSolver {
	public:
		NonlinearSolver(void);
		static SolutionNLE* solve(Function1D *f, float a, float b, float tol);
		~NonlinearSolver();
		SolutionNLE * nleSolution;
	private:
		//SolutionNLE * nleSolution;
	};
	NonlinearSolver::NonlinearSolver()
	: nleSolution(){}
	NonlinearSolver::~NonlinearSolver(){}
//----------------------------------------------------------------------------------
	class NonlinearSolver_bisection : public NonlinearSolver {
	public:
		void acceptNleSolution(SolutionNLE * solution);
	};
	void NonlinearSolver_bisection::acceptNleSolution(SolutionNLE *solution)
	{
		nleSolution = solution;
	}
	SolutionNLE* NonlinearSolver::solve(Function1D *f, float a, float b, float tol)
	{
		SolutionNLE *nleSolution = new SolutionNLE;
		float  fa = f->func(a);
		float  fb = f->func(b);
		float  c  = (a+b)/2;
		int iterations = 1;
		float tolerance = 2 * tol;
		while ((b-a > tolerance ) && (c != a) && (c != b)){
			float fc = f->func(c);
			if ((fc * fa) > 0 ) {
				a = c;
				fa = fc;
			}
			else if (fc != 0){
				b = c;
				fb = fc;
			}
			else {
				nleSolution->setIterations(iterations);
				nleSolution->setFuncValue(f->func(c));
				nleSolution->setSolution(c);
				return nleSolution;
			}
			c = (a + b)/2;
			iterations++;
			
			
		}
		nleSolution->setIterations(iterations);
		nleSolution->setFuncValue(f->func(c));
		nleSolution->setSolution(c);
		return nleSolution;
	}
	
}


All I want to do is seperate namespace 2 into .h/.cpp files for each of classes Nonlinearsolver, Nonlinearsolver_bisection which is a child of nonlinearsolver and SoultionNle. Basically I want to make it like Function1D.h/.cpp whichi is in a namespace but in the respective files.
It's not altogether clear what the problem is.

I hope the the file you've labelled namespace 2 is actually csc350Lib_calculus_snle.h.

Never place using namespace in a header file.
Yes the 2nd namespace is named csc350Lib_calculus_snle.h I want to take the classes that are in that namespace and put them in their own .h/.cpp files but keep getting errors
Topic archived. No new replies allowed.