Class functions undeclared, don't know why!

Hi again everyone,

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

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
#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);


and the .cpp file where it's defined

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
#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=new double [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;
}


I'm trying to use it here:

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
#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.

Thanks,

Mike
'linspace' wasn't declared in the class header file, so it isn't visible on the main source file
Hi Bazzy, is this not a sufficient declaration?

 
friend vectordouble linspace(int a, int b, int num);
No, that just says that 'linspace' is a friend of the class.
OK, how should I declare it?
Define it wherever you would define it normally.
Ah, fixed it. Thanks for the help everyone
Topic archived. No new replies allowed.