undefined error

Hi All,

I am very new to c++ and my tutor has given me a file to use and I cannot get it to compile. I get undefined error and I haven't got a clue how to fix it. It comes as three files. Any help would be greatly appreciated.
euler_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
#include <string>
#include <iostream>
#include <cstdlib>
#include <vector>
#include "euler.h"

int main(int argc, char *argv[]){

  // Get n via the command line

  if (argc==1){ // The first argument is always the program name
    std::cout << "Call via \"euler n\", wher n is an integer." << std::endl;
    return 1;
  }

  int n=atoi(argv[1]); // The second argument is n

  // Define the needed vectors

  std::vector<double> xvals;
  std::vector<double> fvals;
  std::vector<double> err;

  // Perform the computation
  double max_error = my_euler::euler(n, xvals, fvals, err);

  // Output all values
  my_euler::euler_output(xvals,fvals,err);

  // Finally give out the maximum absolute error

  std::cout << "The maximum absolute error is " << max_error << "." << std::endl;
  return 0;
}


euler.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
#include<cmath>
#include<iostream>
#include "euler.h"

// define functions in the right namespace

namespace my_euler {

  double euler(int n, std::vector<double>& xvalues, std::vector<double>& fvalues, std::vector<double>& error){

  // Initialize the vectors

  xvalues.resize(n+1);
  fvalues.resize(n+1);
  error.resize(n+1);

  // Compute the stepsize

  double h=10.0/n; // Be careful to avoid integer division

  // Initialize start values

  xvalues[0]=0;
  fvalues[0]=1;
  error[0]=0;

  double max_error=0; // Will contain maximum error of Euler approximation

  // Now iterate through a loop to compute all values

  for (int i=0;i<n;i++){
    xvalues[i+1]=(i+1)*h; // New x-value
    fvalues[i+1]=fvalues[i]-h*fvalues[i]/c; // New Euler-Approx.
    error[i+1]=std::abs(fvalues[i+1]-exp(-xvalues[i+1]/c)); // Error in step i+1
    max_error=std::max(error[i+1],max_error); // max is defined in cmath
  }

  return max_error; // return value
  }



void euler_output(const std::vector<double>& xvalues, const std::vector<double>& fvalues, const std::vector<double>& error){

  // Output all values

  for (int i=0; i<xvalues.size();i++){
    std::cout << xvalues[i] << " " << fvalues[i] << " " << error[i] << std::endl;
  }
}

}


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

#include<vector>

// Create our own namespace
namespace my_euler{

const double c=4.0; // A global variable

// euler performs the computation
double euler(int n, std::vector<double>& xvalues, std::vector<double>& fvalues,
	     std::vector<double>& error);

// euler_output outputs all values
void euler_output(const std::vector<double>& xvalues, const std::vector<double>& fvalues,
		  const std::vector<double>& error);

}

#endif 


I have used codeblocks and created a new project. Added the files to the project and still undefined my_euler
Last edited on
A general rule about programmers is that we're lazy. You're in for a long wait if you're expecting us to compile your code just to see what the error you got was, so if you want to get an answer anytime soon, you better post the error message.
Thanks for the tip. The error i get is

\Euler\EulerTest\main.cpp|25|undefined reference to `my_euler::euler(int, std::vector<double, std::allocator<double> >&, std::vector<double, std::allocator<double> >&, std::vector<double, std::allocator<double> >&)'|

and

\Euler\EulerTest\main.cpp|28|undefined reference to `my_euler::euler_output(std::vector<double, std::allocator<double> > const&, std::vector<double, std::allocator<double> > const&, std::vector<double, std::allocator<double> > const&)'|

I don't know if that helps?
Compiles and links fine for me. Check that you're actually building the project and not just an isolated file.
Thank you so much for your help. I recieved the program as three seperate files. I tried to open them all and compile them seperately and that didn't work. So I opened a new project and inside that new project there is a main.cpp file. I copied the code from the euler_main.cpp into that file. I then created a new c++ source file inside the sources folder and copied the euler across. I then created a header and did the same again.

Do I need to rename the files? How do I build the project and not the isolated file. I just tried to build workspace and get the same undefined reference. What software do you use?

Thank you again for your time
Topic archived. No new replies allowed.