I'm having trouble with writing functions for my vectordouble class. I'm trying to create a function that essentially does what the Matlab function 'linspace' does, i.e. makes a vector of linearly increasing values. Here's the header file
#ifndef VECTORDEF
#define VECTORDEF
// **********************
// * Class of vectors *
// **********************
// Class written in such a way that code similar to Matlab
// code may be written
// This class throws errors using the class "error"
#include "error.hpp"
#include <math.h>
class vectordouble
{
//private:
// array variables
public:
// constructors
vectordouble(int length_of_vector); // creates vector of given length
vectordouble(); // overwritten default constructor
vectordouble(const vectordouble& v1); // overwritten copy constructor
int vector_size; // length of vector (WAS PRIVATE)
double *x; // pointer to first entry of vector (WAS PRIVATE)
// destructor
~vectordouble();
// A function that linearly divides a given interval into a given number of
// points and assigns them to a vector
friend vectordouble linspace(int a, int b, int num);
#include <iostream>
#include "vectordouble.hpp"
// constructor that creates vector of size length with
// double precision entries all initially set to zero
vectordouble::vectordouble(int length_of_vector)
{
x=newdouble [length_of_vector];
vector_size = length_of_vector;
for (int i=0; i<vector_size; i++)
{
x[i] = 1.0;
}
}
// function prototypes
vectordouble linspace(int a, int b, int num);
// A function that linearly divides a given interval into a given number of
// points and assigns them to a vector
vectordouble linspace(int a, int b, int num)
{
// create a vector of length num
vectordouble v(num);
// now assign the values to the vector
for (int i = 0; i <= num; i++)
{
v.x[i] = a + i * ( (b - a) / num );
}
return v;
}
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <cassert>
#include "vectordouble.hpp"
#include "matrixdouble_new.hpp"
int main()
{
// System parameters:
int x_steps = 10; // Number of points in the spatial domain
int t_steps = 10; // Number of points in the temporal domain
// Define vectors for x and t
vectordouble x(x_steps);
vectordouble t(t_steps);
// Defining the interval bounds
int x_start = -1;
int x_end = 1;
int t_start = 0;
int t_end = 1;
// We need to discretize our time and space domains. We create vectors x and t
// using a 'linspace' function.
x = linspace(x_start,x_end,x_steps);
return 0;
}
The error message is this
1 2 3 4
Source files/heat_1D_CN.cpp: In function `int main()':
Source files/heat_1D_CN.cpp:36: error: `linspace' undeclared (first use this function)
Source files/heat_1D_CN.cpp:36: error: (Each undeclared identifier is reported only once for each function it appears in.)
Does anybody see what's wrong? I've included the header and given a function prototype... So I'm a bit stumped now.