0

I am getting the following error with g++ 4.9:

basis.cpp:16: undefined reference to `Basis::foo(int, int)'

This is the header file:

#ifndef BASIS_H
#define BASIS_H


#include "common.h"
#include <math.h>
#include "xdouble.h"

using namespace std;

class Basis {

private:

    int rank;
    int dim;



public:

    Basis(); //Empty constructor

    Basis(int r, int d); //Default constructor

    void foo(int a, int b);
    void bar(int a, int b);
};

#endif

The basis.cpp file is the following:

#include "basis.h"

Basis::Basis()
{
    rank = 0;
    dim = 0;
}

Basis::Basis(int r, int d) // Default constructor
{
    rank = r;
    dim = d;

}

void Basis::bar(int a, int b)
{
    void foo(int a, int b);
}

void Basis::foo(int a, int b)
{

}

Even though I'm including the basis.h file I get the undefined reference error and I can't understand why this is happening. What am I doing wrong?

Thanks

fc67
  • 409
  • 5
  • 17
  • You probably missed to link `basis.cpp` with your executable. – πάντα ῥεῖ May 15 '15 at 07:40
  • @πάνταῥεῖ this seems like a compile time error though, from looking at the error message. But it is not reproducible from the code above, even removing the `foo` function declaration in `Basis::bar()` and replacing it by a function call. – vsoftco May 15 '15 at 07:44
  • Are you getting a compile time error or a linker error? From your error message it looks like a compile time error, but in the title you say linker error. – vsoftco May 15 '15 at 07:55
  • All files are compiled. Only at the end when the executable is to be created I get this error. So, I assume that this is a linker error – fc67 May 15 '15 at 08:38
  • can you put the commands you use to compile it? – tejas May 15 '15 at 10:31

1 Answers1

1

It looks like a copy and paste error. Try this:

void Basis::bar(int a, int b)
{
  foo(a, b);
}

You made a mistake because you copied and pasted the definition of the function foo in the place where you wanted to call this function.

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
Ahemski
  • 93
  • 8